Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
def predict(message, history):
|
5 |
try:
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Ensure the required library is installed
|
6 |
+
os.system("pip install minijinja")
|
7 |
+
|
8 |
+
"""
|
9 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
10 |
+
"""
|
11 |
+
client = InferenceClient("meta-llama/Meta-Llama-3.1-8B")
|
12 |
+
|
13 |
+
|
14 |
+
def respond(
|
15 |
+
message,
|
16 |
+
history: list[tuple[str, str]],
|
17 |
+
system_message,
|
18 |
+
max_tokens,
|
19 |
+
temperature,
|
20 |
+
top_p,
|
21 |
+
):
|
22 |
+
messages = [system_message]
|
23 |
+
|
24 |
+
for val in history:
|
25 |
+
if val[0]:
|
26 |
+
messages.append(val[0])
|
27 |
+
if val[1]:
|
28 |
+
messages.append(val[1])
|
29 |
+
|
30 |
+
messages.append(message)
|
31 |
+
|
32 |
+
response = ""
|
33 |
|
|
|
34 |
try:
|
35 |
+
for message in client.chat_completion(
|
36 |
+
messages,
|
37 |
+
max_tokens=max_tokens,
|
38 |
+
stream=True,
|
39 |
+
temperature=temperature,
|
40 |
+
top_p=top_p,
|
41 |
+
):
|
42 |
+
token = message.choices[0].delta.content
|
43 |
+
|
44 |
+
response += token
|
45 |
+
yield response
|
46 |
+
except Exception as e:
|
47 |
+
yield f"Error: {str(e)}"
|
48 |
+
|
49 |
+
|
50 |
+
"""
|
51 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
52 |
+
"""
|
53 |
+
demo = gr.ChatInterface(
|
54 |
+
respond,
|
55 |
+
additional_inputs=[
|
56 |
+
gr.Textbox(value="", label="System message"),
|
57 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
58 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
59 |
+
gr.Slider(
|
60 |
+
minimum=0.1,
|
61 |
+
maximum=1.0,
|
62 |
+
value=0.95,
|
63 |
+
step=0.05,
|
64 |
+
label="Top-p (nucleus sampling)",
|
65 |
+
),
|
66 |
+
],
|
67 |
+
)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch()
|