Wosqa commited on
Commit
fbac762
·
verified ·
1 Parent(s): 888bbe8

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +84 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ Explain programming concepts in a simple, beginner-friendly way.
14
+ Provide examples when helpful and keep answers concise.
15
+ """
16
+
17
+ # Build messages safely
18
+ def build_messages(message, chat_history):
19
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
20
+ if chat_history is not None:
21
+ for item in chat_history:
22
+ if isinstance(item, tuple) and len(item) == 2:
23
+ user_msg = str(item[0])
24
+ bot_msg = str(item[1])
25
+ messages.append({"role": "user", "content": user_msg})
26
+ messages.append({"role": "assistant", "content": bot_msg})
27
+ messages.append({"role": "user", "content": str(message)})
28
+ return messages
29
+
30
+ # Respond function for Gradio
31
+ def respond(message, chat_history, temperature):
32
+ # Ensure chat_history is list of tuples
33
+ safe_history = []
34
+ if chat_history is not None:
35
+ for item in chat_history:
36
+ if isinstance(item, tuple) and len(item) == 2:
37
+ safe_history.append((str(item[0]), str(item[1])))
38
+
39
+ messages = build_messages(message, safe_history)
40
+
41
+ headers = {
42
+ "Authorization": f"Bearer {GROQ_API_KEY}",
43
+ "Content-Type": "application/json"
44
+ }
45
+
46
+ response = requests.post(
47
+ GROQ_API_URL,
48
+ headers=headers,
49
+ json={
50
+ "model": MODEL_NAME,
51
+ "messages": messages,
52
+ "temperature": temperature
53
+ }
54
+ )
55
+
56
+ if response.status_code == 200:
57
+ bot_reply = response.json()["choices"][0]["message"]["content"]
58
+ else:
59
+ bot_reply = f"Error {response.status_code}: {response.text}"
60
+
61
+ safe_history.append((str(message), str(bot_reply)))
62
+ return "", safe_history
63
+
64
+ # Gradio UI
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
67
+
68
+ chatbot = gr.Chatbot()
69
+ state = gr.State([])
70
+
71
+ msg = gr.Textbox(label="Ask a programming question")
72
+ temperature = gr.Slider(
73
+ minimum=0.1,
74
+ maximum=1.0,
75
+ value=0.7,
76
+ step=0.1,
77
+ label="Response Creativity"
78
+ )
79
+ clear = gr.Button("Clear Chat")
80
+
81
+ msg.submit(respond, [msg, state, temperature], [msg, chatbot])
82
+ clear.click(lambda: ([], []), None, [chatbot, state])
83
+
84
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio