Wosqa commited on
Commit
e0ee53e
·
verified ·
1 Parent(s): 6b87b35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -13,46 +13,37 @@ Explain programming concepts in a simple, beginner-friendly way.
13
  Provide examples when helpful and keep answers concise.
14
  """
15
 
16
- # Build messages for GROQ API
17
- def build_groq_messages(user_input, chat_history):
18
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
19
-
20
- # Convert Gradio history tuples into dicts for API
21
  for item in chat_history or []:
22
  if isinstance(item, tuple) and len(item) == 2:
23
  user_msg, bot_msg = item
24
- messages.append({"role": "user", "content": str(user_msg)})
25
- messages.append({"role": "assistant", "content": str(bot_msg)})
26
-
27
- # Latest user input
28
- messages.append({"role": "user", "content": str(user_input)})
29
- return messages
30
 
31
- # Call GROQ API
32
- def query_groq(user_input, chat_history, temperature):
33
- messages = build_groq_messages(user_input, chat_history)
34
 
 
35
  headers = {
36
  "Authorization": f"Bearer {GROQ_API_KEY}",
37
  "Content-Type": "application/json"
38
  }
39
-
40
  payload = {
41
  "model": MODEL_NAME,
42
- "messages": messages,
43
  "temperature": temperature
44
  }
45
 
46
  response = requests.post(GROQ_API_URL, headers=headers, json=payload)
47
-
48
  if response.status_code == 200:
49
- return response.json()["choices"][0]["message"]["content"]
50
  else:
51
- return f"Error {response.status_code}: {response.text}"
52
 
53
- # Gradio respond function
54
- def respond(user_input, chat_history, temperature):
55
- bot_reply = query_groq(user_input, chat_history, temperature)
56
  new_history = (chat_history or []) + [(user_input, bot_reply)]
57
  return "", new_history
58
 
 
13
  Provide examples when helpful and keep answers concise.
14
  """
15
 
16
+ # Gradio respond function with safe GROQ formatting
17
+ def respond(user_input, chat_history, temperature):
18
+ # Convert chat history tuples to dicts
19
+ groq_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
20
  for item in chat_history or []:
21
  if isinstance(item, tuple) and len(item) == 2:
22
  user_msg, bot_msg = item
23
+ groq_messages.append({"role": "user", "content": str(user_msg)})
24
+ groq_messages.append({"role": "assistant", "content": str(bot_msg)})
 
 
 
 
25
 
26
+ # Append latest user input
27
+ groq_messages.append({"role": "user", "content": str(user_input)})
 
28
 
29
+ # Call GROQ API
30
  headers = {
31
  "Authorization": f"Bearer {GROQ_API_KEY}",
32
  "Content-Type": "application/json"
33
  }
 
34
  payload = {
35
  "model": MODEL_NAME,
36
+ "messages": groq_messages,
37
  "temperature": temperature
38
  }
39
 
40
  response = requests.post(GROQ_API_URL, headers=headers, json=payload)
 
41
  if response.status_code == 200:
42
+ bot_reply = response.json()["choices"][0]["message"]["content"]
43
  else:
44
+ bot_reply = f"Error {response.status_code}: {response.text}"
45
 
46
+ # Return Gradio chat history as tuples for display
 
 
47
  new_history = (chat_history or []) + [(user_input, bot_reply)]
48
  return "", new_history
49