Spaces:
Runtime error
Runtime error
terminaldz
commited on
Commit
•
50c09ae
1
Parent(s):
31be7b1
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
-
import
|
4 |
-
|
5 |
|
|
|
6 |
TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
7 |
|
8 |
client = OpenAI(
|
9 |
base_url="https://api-inference.huggingface.co/v1/",
|
@@ -20,45 +23,41 @@ def respond(
|
|
20 |
):
|
21 |
messages = [{"role": "system", "content": system_message}]
|
22 |
|
23 |
-
for
|
24 |
-
if
|
25 |
-
messages.append({"role": "user", "content":
|
26 |
-
if
|
27 |
-
messages.append({"role": "assistant", "content":
|
28 |
|
29 |
messages.append({"role": "user", "content": message})
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
additional_inputs=[
|
49 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
from openai import OpenAI
|
4 |
+
from openai.error import BadRequestError
|
|
|
5 |
|
6 |
+
# Retrieve the Hugging Face API token from environment variables
|
7 |
TOKEN = os.getenv("HF_TOKEN")
|
8 |
+
if not TOKEN:
|
9 |
+
raise ValueError("Hugging Face API token (HF_TOKEN) not set in environment variables.")
|
10 |
|
11 |
client = OpenAI(
|
12 |
base_url="https://api-inference.huggingface.co/v1/",
|
|
|
23 |
):
|
24 |
messages = [{"role": "system", "content": system_message}]
|
25 |
|
26 |
+
for user_message, assistant_message in history:
|
27 |
+
if user_message:
|
28 |
+
messages.append({"role": "user", "content": user_message})
|
29 |
+
if assistant_message:
|
30 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
31 |
|
32 |
messages.append({"role": "user", "content": message})
|
33 |
|
34 |
+
try:
|
35 |
+
response = ""
|
36 |
+
for msg in client.chat.completions.create(
|
37 |
+
model="meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
|
38 |
+
max_tokens=max_tokens,
|
39 |
+
stream=True,
|
40 |
+
temperature=temperature,
|
41 |
+
top_p=top_p,
|
42 |
+
messages=messages,
|
43 |
+
):
|
44 |
+
token = msg.choices[0].delta.content
|
45 |
+
response += token
|
46 |
+
yield response
|
47 |
+
except BadRequestError as e:
|
48 |
+
error_message = f"Error: {e}. Please ensure your Hugging Face token is valid and you have a Pro subscription."
|
49 |
+
yield error_message
|
50 |
+
|
51 |
+
# Define the Gradio interface
|
52 |
demo = gr.ChatInterface(
|
53 |
+
fn=respond,
|
54 |
additional_inputs=[
|
55 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
56 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
57 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
58 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
60 |
)
|
61 |
|
|
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|