Wosqa commited on
Commit
b6cfcdc
·
verified ·
1 Parent(s): 6a93b02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -2,51 +2,67 @@ import gradio as gr
2
  import os
3
  import requests
4
 
 
5
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
6
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
7
  MODEL_NAME = "llama3-8b-8192"
8
 
9
- SYSTEM_PROMPT = "You are a Programming Tutor. Answer programming questions clearly."
 
10
 
11
- def respond(user_input, chat_history, temperature):
12
- # Build messages list properly
13
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
14
  for item in chat_history or []:
15
  if isinstance(item, tuple) and len(item) == 2:
16
- messages.append({"role": "user", "content": str(item[0])})
17
- messages.append({"role": "assistant", "content": str(item[1])})
 
 
 
 
 
 
 
 
 
18
  messages.append({"role": "user", "content": str(user_input)})
19
 
20
- # Make API request
21
- response = requests.post(
22
- GROQ_API_URL,
23
- headers={
24
- "Authorization": f"Bearer {GROQ_API_KEY}",
25
- "Content-Type": "application/json"
26
- },
27
- json={
28
- "model": MODEL_NAME,
29
- "messages": messages,
30
- "temperature": float(temperature)
31
- }
32
- )
33
 
34
  if response.status_code == 200:
35
  bot_reply = response.json()["choices"][0]["message"]["content"]
36
  else:
37
  bot_reply = f"Error {response.status_code}: {response.text}"
38
 
39
- # Return tuples for Gradio display
40
  new_history = (chat_history or []) + [(user_input, bot_reply)]
41
  return "", new_history
42
 
 
43
  with gr.Blocks() as demo:
44
- gr.Markdown("## Programming Tutor Chatbot")
 
45
  chatbot = gr.Chatbot()
46
  state = gr.State([])
47
- msg = gr.Textbox(label="Ask a question")
48
- temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Creativity")
 
49
  clear = gr.Button("Clear Chat")
 
50
  msg.submit(respond, [msg, state, temperature], [msg, chatbot])
51
  clear.click(lambda: ([], []), None, [chatbot, state])
 
52
  demo.launch()
 
2
  import os
3
  import requests
4
 
5
+ # Load GROQ API key from Hugging Face Secrets
6
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
7
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
8
  MODEL_NAME = "llama3-8b-8192"
9
 
10
+ # Single-line SYSTEM_PROMPT (no newlines)
11
+ SYSTEM_PROMPT = "You are a Programming Tutor. Explain programming concepts clearly and concisely with examples if needed."
12
 
13
+ # Sanitize chat history into proper dicts
14
+ def sanitize_history(chat_history):
15
+ sanitized = []
16
  for item in chat_history or []:
17
  if isinstance(item, tuple) and len(item) == 2:
18
+ user_msg = str(item[0]) if item[0] is not None else ""
19
+ bot_msg = str(item[1]) if item[1] is not None else ""
20
+ sanitized.append({"role": "user", "content": user_msg})
21
+ sanitized.append({"role": "assistant", "content": bot_msg})
22
+ return sanitized
23
+
24
+ # Respond function for Gradio
25
+ def respond(user_input, chat_history, temperature):
26
+ # Build messages
27
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
28
+ messages += sanitize_history(chat_history)
29
  messages.append({"role": "user", "content": str(user_input)})
30
 
31
+ # Call GROQ API
32
+ headers = {
33
+ "Authorization": f"Bearer {GROQ_API_KEY}",
34
+ "Content-Type": "application/json"
35
+ }
36
+
37
+ payload = {
38
+ "model": MODEL_NAME,
39
+ "messages": messages,
40
+ "temperature": float(temperature)
41
+ }
42
+
43
+ response = requests.post(GROQ_API_URL, headers=headers, json=payload)
44
 
45
  if response.status_code == 200:
46
  bot_reply = response.json()["choices"][0]["message"]["content"]
47
  else:
48
  bot_reply = f"Error {response.status_code}: {response.text}"
49
 
50
+ # Keep Gradio history as tuples for display
51
  new_history = (chat_history or []) + [(user_input, bot_reply)]
52
  return "", new_history
53
 
54
+ # Gradio UI
55
  with gr.Blocks() as demo:
56
+ gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
57
+
58
  chatbot = gr.Chatbot()
59
  state = gr.State([])
60
+
61
+ msg = gr.Textbox(label="Ask a programming question")
62
+ temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Response Creativity")
63
  clear = gr.Button("Clear Chat")
64
+
65
  msg.submit(respond, [msg, state, temperature], [msg, chatbot])
66
  clear.click(lambda: ([], []), None, [chatbot, state])
67
+
68
  demo.launch()