rahideer commited on
Commit
a5070e7
·
verified ·
1 Parent(s): 2dd83f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -11
app.py CHANGED
@@ -5,8 +5,10 @@ import numpy as np
5
  import tempfile
6
  import scipy.io.wavfile
7
  import requests
8
- import openai
9
- OPENAI_API_KEY = os.getenv("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")
 
 
10
 
11
  state = {
12
  "started": False,
@@ -31,21 +33,46 @@ def convert_audio_to_text(audio, lang_choices):
31
  return recog.recognize_google(recorded, language=lang).lower()
32
  except Exception as e:
33
  return f"[Error] Couldn't transcribe: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- def ask_openai(prompt):
 
 
 
 
 
 
36
  headers = {
37
- "Authorization": f"Bearer {OPENAI_API_KEY}",
38
  "Content-Type": "application/json"
39
  }
 
40
  data = {
41
- "model": "gpt-3.5-turbo",
42
- "messages": [{"role": "user", "content": prompt}]
 
43
  }
44
 
45
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=data)
 
46
  if response.status_code == 200:
47
  return response.json()["choices"][0]["message"]["content"]
48
- return "[OpenAI Error]"
 
49
 
50
  def begin_game():
51
  state["started"] = True
@@ -92,7 +119,7 @@ def get_next_question():
92
  {history_prompt}
93
 
94
  Your question:"""
95
- return ask_openai(prompt).strip()
96
 
97
  def generate_final_guess():
98
  history = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
@@ -101,7 +128,7 @@ def generate_final_guess():
101
  {history}
102
 
103
  Your guess:"""
104
- return ask_openai(prompt)
105
 
106
  def hint_response():
107
  if not state["hint_mode"] or not state["started"]:
@@ -109,7 +136,7 @@ def hint_response():
109
  question = state["current_q"]
110
  history = "\n".join([f"{q} - {a}" for q, a in state["history"]])
111
  prompt = f"""The player seems confused by this question: "{question}". Previous Q&A were:\n{history}\n\nGive a helpful, simple hint."""
112
- return ask_openai(prompt)
113
 
114
  def toggle_hint_mode():
115
  state["hint_mode"] = not state["hint_mode"]
 
5
  import tempfile
6
  import scipy.io.wavfile
7
  import requests
8
+
9
+ # Set your API keys here
10
+ os.environ["MISTRAL_API_KEY"] = "R1ISnVkHrj7fSd5Dh6ZSZHqCJhhct0ZR"
11
+ os.environ["GROQ_API_KEY"] = "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"
12
 
13
  state = {
14
  "started": False,
 
33
  return recog.recognize_google(recorded, language=lang).lower()
34
  except Exception as e:
35
  return f"[Error] Couldn't transcribe: {str(e)}"
36
+ def query_llm(api, messages, model=None):
37
+ headers = {
38
+ "Authorization": f"Bearer {os.environ[f'{api}_API_KEY']}",
39
+ "Content-Type": "application/json"
40
+ }
41
+ payload = {
42
+ "messages": messages,
43
+ "model": model or ("llama3-70b-8192" if api == "GROQ" else "mistral-medium")
44
+ }
45
+ endpoint = {
46
+ "MISTRAL": "https://api.mistral.ai/v1/chat/completions",
47
+ "GROQ": "https://api.groq.com/openai/v1/chat/completions"
48
+ }[api]
49
+
50
+ response = requests.post(endpoint, headers=headers, json=payload)
51
 
52
+ if response.status_code == 200:
53
+ return response.json()["choices"][0]["message"]["content"]
54
+ else:
55
+ return f"[{api} Error] {response.status_code} - {response.text}"
56
+
57
+ def ask_mistral(prompt):
58
+ url = "https://api.groq.com/openai/v1/chat/completions"
59
  headers = {
60
+ "Authorization": f"Bearer {os.environ['GROQ_API_KEY']}",
61
  "Content-Type": "application/json"
62
  }
63
+
64
  data = {
65
+ "model": "mistral-7b-8k",
66
+ "messages": [{"role": "user", "content": prompt}],
67
+ "temperature": 0.7
68
  }
69
 
70
+ response = requests.post(url, headers=headers, json=data)
71
+
72
  if response.status_code == 200:
73
  return response.json()["choices"][0]["message"]["content"]
74
+ else:
75
+ return f"[Mistral Error] {response.status_code} - {response.text}"
76
 
77
  def begin_game():
78
  state["started"] = True
 
119
  {history_prompt}
120
 
121
  Your question:"""
122
+ return ask_mistral(prompt).strip()
123
 
124
  def generate_final_guess():
125
  history = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
 
128
  {history}
129
 
130
  Your guess:"""
131
+ return ask_mistral(prompt)
132
 
133
  def hint_response():
134
  if not state["hint_mode"] or not state["started"]:
 
136
  question = state["current_q"]
137
  history = "\n".join([f"{q} - {a}" for q, a in state["history"]])
138
  prompt = f"""The player seems confused by this question: "{question}". Previous Q&A were:\n{history}\n\nGive a helpful, simple hint."""
139
+ return ask_mistral(prompt)
140
 
141
  def toggle_hint_mode():
142
  state["hint_mode"] = not state["hint_mode"]