Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
# Set up Groq API key
|
6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
7 |
+
if not GROQ_API_KEY:
|
8 |
+
raise ValueError("GROQ_API_KEY environment variable not set.")
|
9 |
+
|
10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
11 |
+
|
12 |
+
# System prompt
|
13 |
+
SYSTEM_PROMPT = (
|
14 |
+
"You are an intelligent, friendly, and highly adaptable Teaching Assistant Chatbot. "
|
15 |
+
"Your mission is to help users of all ages and skill levels—from complete beginners to seasoned professionals—learn Python, Data Science, and Artificial Intelligence. "
|
16 |
+
"You explain concepts clearly using real-world analogies, examples, and interactive exercises. "
|
17 |
+
"You ask questions to assess the learner’s level, adapt accordingly, and provide learning paths tailored to their pace and goals. "
|
18 |
+
"Your responses are structured, engaging, and supportive. "
|
19 |
+
"You can explain code snippets, generate exercises and quizzes, and recommend projects. "
|
20 |
+
"You never overwhelm users with jargon. Instead, you scaffold complex concepts in simple, digestible steps."
|
21 |
+
)
|
22 |
+
|
23 |
+
def chat_with_groq(user_input, user_data):
|
24 |
+
messages = [
|
25 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
26 |
+
{"role": "user", "content": user_input}
|
27 |
+
]
|
28 |
+
chat_completion = client.chat.completions.create(
|
29 |
+
messages=messages,
|
30 |
+
model="llama-3.3-70b-versatile",
|
31 |
+
stream=False
|
32 |
+
)
|
33 |
+
return chat_completion.choices[0].message.content
|
34 |
+
|
35 |
+
def user_onboarding(age, goals, knowledge_level):
|
36 |
+
return (
|
37 |
+
"Welcome! Based on your goals and knowledge level, we will tailor the learning experience for you.\n"
|
38 |
+
"Let's start learning Python, Data Science, or AI!"
|
39 |
+
)
|
40 |
+
|
41 |
+
def chatbot_interface(age, goals, knowledge_level, user_message):
|
42 |
+
if not age or not goals or not knowledge_level:
|
43 |
+
return user_onboarding(age, goals, knowledge_level)
|
44 |
+
user_data = {
|
45 |
+
"age": age,
|
46 |
+
"goals": goals,
|
47 |
+
"knowledge_level": knowledge_level
|
48 |
+
}
|
49 |
+
return chat_with_groq(user_message, user_data)
|
50 |
+
|
51 |
+
def create_chatbot():
|
52 |
+
with gr.Blocks(css="""
|
53 |
+
.gradio-container { background-color: #f0f4f8; font-family: 'Segoe UI', sans-serif; }
|
54 |
+
#title { font-size: 32px; font-weight: bold; text-align: center; padding-top: 20px; color: #2c3e50; }
|
55 |
+
#subtitle { font-size: 18px; text-align: center; margin-bottom: 20px; color: #34495e; }
|
56 |
+
.input-card, .output-card {
|
57 |
+
background-color: white;
|
58 |
+
padding: 20px;
|
59 |
+
border-radius: 12px;
|
60 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.05);
|
61 |
+
margin-bottom: 20px;
|
62 |
+
}
|
63 |
+
.gr-button { font-size: 16px !important; padding: 10px 20px !important; }
|
64 |
+
""") as demo:
|
65 |
+
|
66 |
+
gr.HTML("<div id='title'>🎓 AI Teaching Assistant Chatbot</div>")
|
67 |
+
gr.HTML("<div id='subtitle'>Helping you learn Python, Data Science & AI at your pace!</div>")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(elem_classes=["input-card"]):
|
71 |
+
age_input = gr.Textbox(label="Age", placeholder="e.g. 20", interactive=True)
|
72 |
+
goals_input = gr.Textbox(label="Learning Goals", placeholder="e.g. I want to become a data analyst", interactive=True)
|
73 |
+
knowledge_level_input = gr.Dropdown(
|
74 |
+
choices=["Beginner", "Intermediate", "Advanced"],
|
75 |
+
label="Knowledge Level",
|
76 |
+
interactive=True
|
77 |
+
)
|
78 |
+
|
79 |
+
with gr.Column(elem_classes=["input-card"]):
|
80 |
+
user_message_input = gr.Textbox(
|
81 |
+
label="Your Question or Message",
|
82 |
+
placeholder="Ask anything about Python, Data Science, AI...",
|
83 |
+
lines=5,
|
84 |
+
interactive=True
|
85 |
+
)
|
86 |
+
|
87 |
+
with gr.Column(elem_classes=["output-card"]):
|
88 |
+
chatbot_output = gr.Textbox(
|
89 |
+
label="Chatbot Response",
|
90 |
+
placeholder="Chatbot response will appear here.",
|
91 |
+
lines=12,
|
92 |
+
max_lines=30,
|
93 |
+
interactive=False
|
94 |
+
)
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
submit_button = gr.Button("Submit", variant="primary")
|
98 |
+
clear_button = gr.Button("Clear", variant="secondary")
|
99 |
+
|
100 |
+
submit_button.click(
|
101 |
+
chatbot_interface,
|
102 |
+
inputs=[age_input, goals_input, knowledge_level_input, user_message_input],
|
103 |
+
outputs=chatbot_output
|
104 |
+
)
|
105 |
+
|
106 |
+
clear_button.click(
|
107 |
+
fn=lambda: ("", "", "", "", ""),
|
108 |
+
inputs=[],
|
109 |
+
outputs=[age_input, goals_input, knowledge_level_input, user_message_input, chatbot_output]
|
110 |
+
)
|
111 |
+
|
112 |
+
demo.launch()
|
113 |
+
|
114 |
+
# Run the chatbot
|
115 |
+
create_chatbot()
|