Spaces:
Running
Running
test
Browse files- app.py +46 -15
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,10 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
-
|
6 |
"""
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def respond(
|
@@ -25,19 +31,44 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
"""
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
|
6 |
"""
|
7 |
+
Using Together AI API for chat completions
|
8 |
"""
|
9 |
+
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
10 |
+
if not TOGETHER_API_KEY:
|
11 |
+
raise ValueError("TOGETHER_API_KEY environment variable is not set")
|
12 |
+
|
13 |
+
TOGETHER_API_URL = "https://api.together.xyz/v1/chat/completions"
|
14 |
|
15 |
|
16 |
def respond(
|
|
|
31 |
|
32 |
messages.append({"role": "user", "content": message})
|
33 |
|
34 |
+
headers = {
|
35 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
36 |
+
"Content-Type": "application/json"
|
37 |
+
}
|
38 |
+
|
39 |
+
data = {
|
40 |
+
"model": "arcee-ai/AFM-4.5B",
|
41 |
+
"messages": messages,
|
42 |
+
"max_tokens": max_tokens,
|
43 |
+
"temperature": temperature,
|
44 |
+
"top_p": top_p,
|
45 |
+
"stream": True
|
46 |
+
}
|
47 |
|
48 |
+
response = ""
|
49 |
+
|
50 |
+
try:
|
51 |
+
with requests.post(TOGETHER_API_URL, headers=headers, json=data, stream=True) as r:
|
52 |
+
r.raise_for_status()
|
53 |
+
for line in r.iter_lines():
|
54 |
+
if line:
|
55 |
+
line = line.decode('utf-8')
|
56 |
+
if line.startswith('data: '):
|
57 |
+
line = line[6:] # Remove 'data: ' prefix
|
58 |
+
if line.strip() == '[DONE]':
|
59 |
+
break
|
60 |
+
try:
|
61 |
+
chunk = json.loads(line)
|
62 |
+
if 'choices' in chunk and len(chunk['choices']) > 0:
|
63 |
+
delta = chunk['choices'][0].get('delta', {})
|
64 |
+
if 'content' in delta:
|
65 |
+
token = delta['content']
|
66 |
+
response += token
|
67 |
+
yield response
|
68 |
+
except json.JSONDecodeError:
|
69 |
+
continue
|
70 |
+
except requests.exceptions.RequestException as e:
|
71 |
+
yield f"Error: {str(e)}"
|
72 |
|
73 |
|
74 |
"""
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|