Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,63 @@
|
|
|
|
1 |
import requests
|
2 |
import os
|
3 |
|
4 |
-
api_key=os.environ.get("HYPERBOLIC_API_KEY")
|
5 |
url = "https://api.hyperbolic.xyz/v1/chat/completions"
|
6 |
|
7 |
def hyperbolic(prompt):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
3 |
import os
|
4 |
|
5 |
+
api_key = os.environ.get("HYPERBOLIC_API_KEY")
|
6 |
url = "https://api.hyperbolic.xyz/v1/chat/completions"
|
7 |
|
8 |
def hyperbolic(prompt):
|
9 |
+
|
10 |
+
try:
|
11 |
+
headers = {
|
12 |
+
"Content-Type": "application/json",
|
13 |
+
"Authorization": f"Bearer {api_key}"
|
14 |
+
}
|
15 |
+
data = {
|
16 |
+
"messages": [
|
17 |
+
{
|
18 |
+
"role": "user",
|
19 |
+
"content": f"{prompt}"
|
20 |
+
}
|
21 |
+
],
|
22 |
+
"model": "Qwen/Qwen2.5-72B-Instruct",
|
23 |
+
"max_tokens": 32768,
|
24 |
+
"temperature": 0.2,
|
25 |
+
"top_p": 0.9
|
26 |
+
}
|
27 |
+
|
28 |
+
response = requests.post(url, headers=headers, json=data)
|
29 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
30 |
+
json_response = response.json()
|
31 |
+
|
32 |
+
# Extract the response text from the JSON
|
33 |
+
if 'choices' in json_response and len(json_response['choices']) > 0:
|
34 |
+
return json_response['choices'][0]['message']['content']
|
35 |
+
else:
|
36 |
+
return f"Error: Unexpected response format from Hyperbolic API: {json_response}"
|
37 |
+
|
38 |
+
except requests.exceptions.RequestException as e:
|
39 |
+
return f"Error: Could not connect to Hyperbolic API: {e}"
|
40 |
+
except (KeyError, TypeError) as e:
|
41 |
+
return f"Error: Problem parsing Hyperbolic API response: {e}"
|
42 |
+
except Exception as e:
|
43 |
+
return f"An unexpected error occurred: {e}"
|
44 |
+
|
45 |
+
|
46 |
+
def chat(message, history):
|
47 |
+
|
48 |
+
bot_message = hyperbolic(message)
|
49 |
+
history.append((message, bot_message))
|
50 |
+
return history
|
51 |
+
|
52 |
+
if __name__ == '__main__':
|
53 |
+
if not api_key:
|
54 |
+
print("Error: HYPERBOLIC_API_KEY environment variable not set. Please set it before running.")
|
55 |
+
else:
|
56 |
+
demo = gr.ChatInterface(
|
57 |
+
fn=chat,
|
58 |
+
title="Hyperbolic Chatbot",
|
59 |
+
description="A chatbot powered by the Hyperbolic API.",
|
60 |
+
examples=["Tell me a joke.", "What is the capital of France?", "Summarize the plot of Hamlet."],
|
61 |
+
chatbot=gr.Chatbot(height=600) # Adjust height as needed
|
62 |
+
)
|
63 |
+
demo.launch()
|