Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 7 |
+
|
| 8 |
+
def generate_tutor_output(subject, difficulty, student_input):
|
| 9 |
+
prompt = f"""
|
| 10 |
+
You are an expert tutor in {subject} at the {difficulty} level.
|
| 11 |
+
The student has provided the following input: "{student_input}"
|
| 12 |
+
|
| 13 |
+
Please generate:
|
| 14 |
+
1. A brief, engaging lesson on the topic (2-3 paragraphs)
|
| 15 |
+
2. A thought-provoking question to check understanding
|
| 16 |
+
3. Constructive feedback on the student's input
|
| 17 |
+
|
| 18 |
+
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
completion = client.chat.completions.create(
|
| 22 |
+
messages=[
|
| 23 |
+
{
|
| 24 |
+
"role": "system",
|
| 25 |
+
"content": "You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"role": "user",
|
| 29 |
+
"content": prompt,
|
| 30 |
+
}
|
| 31 |
+
],
|
| 32 |
+
model="llama3-groq-8b-8192-tool-use-preview",
|
| 33 |
+
max_tokens=1000,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return completion.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("# 🎓 Your AI Tutor by Farhan")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column(scale=2):
|
| 43 |
+
subject = gr.Dropdown(
|
| 44 |
+
["Math", "Science", "History", "Literature", "Code", "AI"],
|
| 45 |
+
label="Subject",
|
| 46 |
+
info="Choose the subject of your lesson"
|
| 47 |
+
)
|
| 48 |
+
difficulty = gr.Radio(
|
| 49 |
+
["Beginner", "Intermediate", "Advanced"],
|
| 50 |
+
label="Difficulty Level",
|
| 51 |
+
info="Select your proficiency level"
|
| 52 |
+
)
|
| 53 |
+
student_input = gr.Textbox(
|
| 54 |
+
placeholder="Type your query here...",
|
| 55 |
+
label="Your Input",
|
| 56 |
+
info="Enter the topic you want to learn"
|
| 57 |
+
)
|
| 58 |
+
submit_button = gr.Button("Generate Lesson", variant="primary")
|
| 59 |
+
|
| 60 |
+
with gr.Column(scale=3):
|
| 61 |
+
lesson_output = gr.Markdown(label="Lesson")
|
| 62 |
+
question_output = gr.Markdown(label="Comprehension Question")
|
| 63 |
+
feedback_output = gr.Markdown(label="Feedback")
|
| 64 |
+
|
| 65 |
+
gr.Markdown("""
|
| 66 |
+
### How to Use
|
| 67 |
+
1. Select a subject from the dropdown.
|
| 68 |
+
2. Choose your difficulty level.
|
| 69 |
+
3. Enter the topic or question you'd like to explore.
|
| 70 |
+
4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
|
| 71 |
+
5. Review the AI-generated content to enhance your learning.
|
| 72 |
+
6. Feel free to ask follow-up questions or explore new topics!
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
def process_output(output):
|
| 76 |
+
try:
|
| 77 |
+
parsed = eval(output)
|
| 78 |
+
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 79 |
+
except:
|
| 80 |
+
return "Error parsing output", "No question available", "No feedback available"
|
| 81 |
+
|
| 82 |
+
submit_button.click(
|
| 83 |
+
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
|
| 84 |
+
inputs=[subject, difficulty, student_input],
|
| 85 |
+
outputs=[lesson_output, question_output, feedback_output]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|