Wosqa commited on
Commit
fda5ed5
·
verified ·
1 Parent(s): 7cbc1ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -73
app.py CHANGED
@@ -1,74 +1,71 @@
1
- import gradio as gr
2
- import os
3
- import requests
4
-
5
- # Load GROQ API key from environment (Hugging Face Secrets)
6
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
7
-
8
- GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
9
- MODEL_NAME = "llama3-8b-8192"
10
-
11
- # 🎯 Customize System Prompt (CHANGE THEME IF YOU WANT)
12
- SYSTEM_PROMPT = """
13
- You are an expert Programming Tutor.
14
- You explain programming concepts in a simple, clear, and beginner-friendly way.
15
- Provide examples when helpful and keep answers concise.
16
- """
17
-
18
- def query_groq(message, chat_history, temperature):
19
- headers = {
20
- "Authorization": f"Bearer {GROQ_API_KEY}",
21
- "Content-Type": "application/json"
22
- }
23
-
24
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
25
-
26
- for user, bot in chat_history:
27
- messages.append({"role": "user", "content": user})
28
- messages.append({"role": "assistant", "content": bot})
29
-
30
- messages.append({"role": "user", "content": message})
31
-
32
- response = requests.post(
33
- GROQ_API_URL,
34
- headers=headers,
35
- json={
36
- "model": MODEL_NAME,
37
- "messages": messages,
38
- "temperature": temperature
39
- }
40
- )
41
-
42
- if response.status_code == 200:
43
- return response.json()["choices"][0]["message"]["content"]
44
- else:
45
- return f"Error {response.status_code}: {response.text}"
46
-
47
- def respond(message, chat_history, temperature):
48
- bot_reply = query_groq(message, chat_history, temperature)
49
- chat_history.append((message, bot_reply))
50
- return "", chat_history
51
-
52
- with gr.Blocks() as demo:
53
- gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
54
-
55
- chatbot = gr.Chatbot()
56
- state = gr.State([])
57
-
58
- msg = gr.Textbox(label="Ask a programming question")
59
-
60
- # ✅ UI Improvement (Slider)
61
- temperature = gr.Slider(
62
- minimum=0.1,
63
- maximum=1.0,
64
- value=0.7,
65
- step=0.1,
66
- label="Response Creativity"
67
- )
68
-
69
- clear = gr.Button("Clear Chat")
70
-
71
- msg.submit(respond, [msg, state, temperature], [msg, chatbot])
72
- clear.click(lambda: ([], []), None, [chatbot, state])
73
-
74
  demo.launch()
 
1
+ import gradio as gr
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
+ # System prompt: defines chatbot personality
11
+ SYSTEM_PROMPT = """
12
+ You are an expert Programming Tutor.
13
+ You explain programming concepts in a simple, clear, and beginner-friendly way.
14
+ Provide examples when helpful and keep answers concise.
15
+ """
16
+
17
+ # Function to call GROQ API
18
+ def query_groq(message, chat_history, temperature):
19
+ headers = {
20
+ "Authorization": f"Bearer {GROQ_API_KEY}",
21
+ "Content-Type": "application/json"
22
+ }
23
+
24
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
25
+ for user, bot in chat_history:
26
+ messages.append({"role": "user", "content": user})
27
+ messages.append({"role": "assistant", "content": bot})
28
+ messages.append({"role": "user", "content": message})
29
+
30
+ response = requests.post(
31
+ GROQ_API_URL,
32
+ headers=headers,
33
+ json={
34
+ "model": MODEL_NAME,
35
+ "messages": messages,
36
+ "temperature": temperature
37
+ }
38
+ )
39
+
40
+ if response.status_code == 200:
41
+ return response.json()["choices"][0]["message"]["content"]
42
+ else:
43
+ return f"Error {response.status_code}: {response.text}"
44
+
45
+ # Function called when user submits a message
46
+ def respond(message, chat_history, temperature):
47
+ bot_reply = query_groq(message, chat_history, temperature)
48
+ chat_history.append((message, bot_reply))
49
+ return "", chat_history # return empty textbox + updated chat history
50
+
51
+ # Create Gradio UI
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
54
+
55
+ chatbot = gr.Chatbot() # Chat window
56
+ state = gr.State([]) # Stores chat history
57
+ msg = gr.Textbox(label="Ask a programming question") # User input
58
+ temperature = gr.Slider(
59
+ minimum=0.1,
60
+ maximum=1.0,
61
+ value=0.7,
62
+ step=0.1,
63
+ label="Response Creativity"
64
+ )
65
+ clear = gr.Button("Clear Chat")
66
+
67
+ # Connect submit and button
68
+ msg.submit(respond, [msg, state, temperature], [msg, chatbot])
69
+ clear.click(lambda: ([], []), None, [chatbot, state])
70
+
 
 
 
71
  demo.launch()