Alyosha11 commited on
Commit
cae7c7d
·
1 Parent(s): fda441e
Files changed (2) hide show
  1. app.py +46 -15
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,10 +1,16 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
  """
5
- 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
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
8
 
9
 
10
  def respond(
@@ -25,19 +31,44 @@ def respond(
25
 
26
  messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- huggingface_hub==0.25.2
 
 
1
+ gradio
2
+ requests