Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,74 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
|
|
|
4 |
|
5 |
-
|
6 |
-
api_token = os.getenv("HUGGINGFACE_API_TOKEN_V")
|
7 |
print('----------------',api_token,'-----------------')
|
8 |
# Check if the API token is set
|
9 |
if not api_token:
|
10 |
raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
|
6 |
+
TOKEN = os.getenv("HUGGINGFACE_API_TOKEN_V")
|
|
|
7 |
print('----------------',api_token,'-----------------')
|
8 |
# Check if the API token is set
|
9 |
if not api_token:
|
10 |
raise ValueError("API token is not set. Please set the HUGGINGFACE_API_TOKEN environment variable.")
|
11 |
+
import gradio as gr
|
12 |
+
import requests
|
13 |
+
import os
|
14 |
+
|
15 |
+
def respond(
|
16 |
+
message,
|
17 |
+
history: list[tuple[str, str]],
|
18 |
+
system_message,
|
19 |
+
max_tokens,
|
20 |
+
temperature,
|
21 |
+
top_p,
|
22 |
+
):
|
23 |
+
messages = [{"role": "system", "content": system_message}]
|
24 |
+
|
25 |
+
for val in history:
|
26 |
+
if val[0]:
|
27 |
+
messages.append({"role": "user", "content": val[0]})
|
28 |
+
if val[1]:
|
29 |
+
messages.append({"role": "assistant", "content": val[1]})
|
30 |
+
|
31 |
+
messages.append({"role": "user", "content": message})
|
32 |
+
|
33 |
+
headers = {
|
34 |
+
"Authorization": f"Bearer {TOKEN}",
|
35 |
+
"Content-Type": "application/json"
|
36 |
+
}
|
37 |
+
|
38 |
+
payload = {
|
39 |
+
"model": "meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
|
40 |
+
"max_tokens": max_tokens,
|
41 |
+
"temperature": temperature,
|
42 |
+
"top_p": top_p,
|
43 |
+
"messages": messages
|
44 |
+
}
|
45 |
+
|
46 |
+
response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", headers=headers, json=payload, stream=True)
|
47 |
+
|
48 |
+
response_text = ""
|
49 |
+
for chunk in response.iter_content(chunk_size=None):
|
50 |
+
if chunk:
|
51 |
+
response_text += chunk.decode('utf-8')
|
52 |
+
yield response_text
|
53 |
+
|
54 |
+
theme="Nymbo/Nymbo_Theme"
|
55 |
|
56 |
+
demo = gr.ChatInterface(
|
57 |
+
respond,
|
58 |
+
theme=theme,
|
59 |
+
additional_inputs=[
|
60 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
61 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
62 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
63 |
+
gr.Slider(
|
64 |
+
minimum=0.1,
|
65 |
+
maximum=1.0,
|
66 |
+
value=0.95,
|
67 |
+
step=0.05,
|
68 |
+
label="Top-p (nucleus sampling)",
|
69 |
+
),
|
70 |
+
],
|
71 |
)
|
72 |
|
73 |
+
if __name__ == "__main__":
|
74 |
+
demo.launch()
|