Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -26,26 +38,24 @@ def respond(
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
for message in
|
| 31 |
-
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
|
|
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
respond,
|
| 47 |
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="
|
| 49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
gr.Slider(
|
|
@@ -53,12 +63,12 @@ demo = gr.ChatInterface(
|
|
| 53 |
maximum=1.0,
|
| 54 |
value=0.95,
|
| 55 |
step=0.05,
|
| 56 |
-
label="Top-
|
| 57 |
),
|
|
|
|
| 58 |
],
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch(
|
| 64 |
-
|
|
|
|
| 1 |
+
#refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
|
| 2 |
+
#huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
|
| 3 |
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
css = '''
|
| 8 |
+
.gradio-container{max-width: 1000px !important}
|
| 9 |
+
h1{text-align:center}
|
| 10 |
+
footer {
|
| 11 |
+
visibility: hidden
|
| 12 |
+
}
|
| 13 |
+
'''
|
| 14 |
|
| 15 |
+
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
+
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
| 19 |
+
api_key=ACCESS_TOKEN,
|
| 20 |
+
)
|
| 21 |
|
| 22 |
def respond(
|
| 23 |
message,
|
|
|
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
response = ""
|
| 41 |
+
|
| 42 |
+
for message in client.chat.completions.create(
|
| 43 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 44 |
max_tokens=max_tokens,
|
| 45 |
stream=True,
|
| 46 |
temperature=temperature,
|
| 47 |
top_p=top_p,
|
| 48 |
+
messages=messages,
|
| 49 |
):
|
| 50 |
token = message.choices[0].delta.content
|
| 51 |
+
|
| 52 |
response += token
|
| 53 |
yield response
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
respond,
|
| 57 |
additional_inputs=[
|
| 58 |
+
gr.Textbox(value="", label="System message"),
|
| 59 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 60 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 61 |
gr.Slider(
|
|
|
|
| 63 |
maximum=1.0,
|
| 64 |
value=0.95,
|
| 65 |
step=0.05,
|
| 66 |
+
label="Top-P",
|
| 67 |
),
|
| 68 |
+
|
| 69 |
],
|
| 70 |
+
css=css,
|
| 71 |
+
theme="allenai/gradio-theme",
|
| 72 |
)
|
|
|
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|
|
|