Laibaaaaa commited on
Commit
cf4a6e5
·
verified ·
1 Parent(s): 996ef4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -64
app.py CHANGED
@@ -1,64 +1,62 @@
1
- import gradio as gr
2
- import os
3
- import requests
4
-
5
- # Read API key from environment (Hugging Face will provide it)
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
- # 🧠 This defines what your chatbot is
12
- SYSTEM_PROMPT = """
13
- You are a friendly Data Science Tutor.
14
- You help students understand data science, machine learning,
15
- statistics, Python, and big data concepts in simple words.
16
- """
17
-
18
- def query_groq(user_message, chat_history):
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": user_message})
31
-
32
- response = requests.post(
33
- GROQ_API_URL,
34
- headers=headers,
35
- json={
36
- "model": MODEL_NAME,
37
- "messages": messages,
38
- "temperature": 0.7
39
- }
40
- )
41
-
42
- if response.status_code == 200:
43
- return response.json()["choices"][0]["message"]["content"]
44
- else:
45
- return "Error connecting to GROQ API"
46
-
47
- def respond(message, chat_history):
48
- reply = query_groq(message, chat_history)
49
- chat_history.append((message, reply))
50
- return "", chat_history
51
-
52
- with gr.Blocks() as demo:
53
- gr.Markdown("## 📊 Data Science Tutor Chatbot")
54
-
55
- chatbot = gr.Chatbot()
56
- msg = gr.Textbox(label="Ask your question")
57
- clear = gr.Button("Clear Chat")
58
-
59
- state = gr.State([])
60
-
61
- msg.submit(respond, [msg, state], [msg, chatbot])
62
- clear.click(lambda: ([], []), None, [chatbot, state])
63
-
64
- demo.launch()
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+
5
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
6
+
7
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
8
+ MODEL_NAME = "llama3-8b-8192"
9
+
10
+ SYSTEM_PROMPT = """
11
+ You are a friendly Data Science Tutor.
12
+ You explain data science, machine learning,
13
+ statistics, Python, and big data concepts clearly.
14
+ """
15
+
16
+ def query_groq(user_message, chat_history):
17
+ headers = {
18
+ "Authorization": f"Bearer {GROQ_API_KEY}",
19
+ "Content-Type": "application/json"
20
+ }
21
+
22
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
23
+
24
+ for user, bot in chat_history:
25
+ messages.append({"role": "user", "content": user})
26
+ messages.append({"role": "assistant", "content": bot})
27
+
28
+ messages.append({"role": "user", "content": user_message})
29
+
30
+ response = requests.post(
31
+ GROQ_API_URL,
32
+ headers=headers,
33
+ json={
34
+ "model": MODEL_NAME,
35
+ "messages": messages,
36
+ "temperature": 0.7
37
+ }
38
+ )
39
+
40
+ if response.status_code == 200:
41
+ return response.json()["choices"][0]["message"]["content"]
42
+ else:
43
+ return "❌ Error connecting to GROQ API"
44
+
45
+ def respond(message, chat_history):
46
+ reply = query_groq(message, chat_history)
47
+ chat_history.append((message, reply))
48
+ return chat_history, chat_history
49
+
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("## 📊 Data Science Tutor Chatbot")
52
+
53
+ chatbot = gr.Chatbot()
54
+ msg = gr.Textbox(label="Ask your question")
55
+ clear = gr.Button("Clear Chat")
56
+
57
+ state = gr.State([])
58
+
59
+ msg.submit(respond, [msg, state], [chatbot, state])
60
+ clear.click(lambda: ([], []), None, [chatbot, state])
61
+
62
+ demo.launch()