13ilguun commited on
Commit
76e6aeb
·
1 Parent(s): 3770d0d

Refactor API integration and update model details in app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -2,20 +2,30 @@
2
  import os
3
  import requests
4
  import gradio as gr
 
5
 
6
- API_URL = "https://router.huggingface.co/together/v1/chat/completions"
 
 
 
7
  headers = {
8
- "Authorization": f"Bearer {os.environ['huggingface_token']}",
9
  }
10
 
11
  def query_api(message):
12
  payload = {
13
  "messages": [{"role": "user", "content": message}],
14
- "model": "mistralai/Mistral-7B-Instruct-v0.3"
15
  }
16
  response = requests.post(API_URL, headers=headers, json=payload)
17
  try:
18
- return response.json()["choices"][0]["message"]["content"]
 
 
 
 
 
 
19
  except Exception as e:
20
  return f"Error: {e}\n\nFull response:\n{response.text}"
21
 
@@ -23,7 +33,7 @@ demo = gr.Interface(
23
  fn=query_api,
24
  inputs=gr.Textbox(label="Ask something"),
25
  outputs="text",
26
- title="Chat with AI Co-Pilot (Mistral 7B Instruct)",
27
  )
28
 
29
  if __name__ == "__main__":
 
2
  import os
3
  import requests
4
  import gradio as gr
5
+ from dotenv import load_dotenv
6
 
7
+ load_dotenv()
8
+ token = os.getenv("TOGETHER_API_KEY")
9
+
10
+ API_URL = "https://api.together.xyz/v1/chat/completions"
11
  headers = {
12
+ "Authorization": f"Bearer {token}",
13
  }
14
 
15
  def query_api(message):
16
  payload = {
17
  "messages": [{"role": "user", "content": message}],
18
+ "model": "mistralai/Mixtral-8x7B-Instruct-v0.1"
19
  }
20
  response = requests.post(API_URL, headers=headers, json=payload)
21
  try:
22
+ result = response.json()
23
+ if "choices" in result:
24
+ return result["choices"][0]["message"]["content"]
25
+ elif "error" in result:
26
+ return f"Error: {result['error']}"
27
+ else:
28
+ return str(result)
29
  except Exception as e:
30
  return f"Error: {e}\n\nFull response:\n{response.text}"
31
 
 
33
  fn=query_api,
34
  inputs=gr.Textbox(label="Ask something"),
35
  outputs="text",
36
+ title="Chat with AI Co-Pilot (Llama 3.1)",
37
  )
38
 
39
  if __name__ == "__main__":