Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,111 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize Groq client
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 16 |
-
3. Give
|
| 17 |
-
|
| 18 |
-
Format your response as a JSON object with keys: "lesson", "
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
gr.
|
| 40 |
-
|
| 41 |
-
with gr.Row():
|
| 42 |
-
with gr.Column(scale=2):
|
| 43 |
-
subject = gr.Dropdown(
|
| 44 |
-
["Math", "Science", "History", "Literature"],
|
| 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 response or question here...",
|
| 55 |
-
label="Your Input",
|
| 56 |
-
info="Enter the topic you want to explore"
|
| 57 |
-
)
|
| 58 |
-
submit_button = gr.Button("📚 Generate Lesson", variant="primary")
|
| 59 |
|
| 60 |
-
with gr.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
|
|
|
| 89 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
+
import logging
|
| 6 |
+
from typing import Dict, Any, Tuple
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 10 |
|
| 11 |
# Initialize Groq client
|
| 12 |
+
try:
|
| 13 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 14 |
+
except KeyError:
|
| 15 |
+
logging.error("GROQ_API_KEY not found in environment variables")
|
| 16 |
+
raise EnvironmentError("Please set the GROQ_API_KEY environment variable")
|
| 17 |
|
| 18 |
+
def generate_tutor_output(subject: str, difficulty: str, student_input: str) -> str:
|
| 19 |
prompt = f"""
|
| 20 |
+
You are an expert tutor in {subject} at the {difficulty} level. The student has provided the following input: "{student_input}"
|
|
|
|
|
|
|
| 21 |
Please generate:
|
| 22 |
1. A brief, engaging lesson on the topic (2-3 paragraphs)
|
| 23 |
+
2. A thought-provoking example with answers to check understanding
|
| 24 |
+
3. Give a real-world problem that can be solved with the lesson
|
| 25 |
+
|
| 26 |
+
Format your response as a JSON object with keys: "lesson", "example", "real_world_problem"
|
| 27 |
"""
|
| 28 |
|
| 29 |
+
try:
|
| 30 |
+
completion = client.chat.completions.create(
|
| 31 |
+
messages=[
|
| 32 |
+
{
|
| 33 |
+
"role": "system",
|
| 34 |
+
"content": f"You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way. 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.",
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": prompt,
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
model="llama3-groq-8b-8192-tool-use-preview",
|
| 42 |
+
max_tokens=1000,
|
| 43 |
+
)
|
| 44 |
+
return completion.choices[0].message.content
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logging.error(f"Error generating tutor output: {str(e)}")
|
| 47 |
+
return json.dumps({"error": "Failed to generate tutor output. Please try again later."})
|
| 48 |
|
| 49 |
+
def process_output(output: str) -> Tuple[str, str, str]:
|
| 50 |
+
try:
|
| 51 |
+
parsed = json.loads(output)
|
| 52 |
+
return (
|
| 53 |
+
parsed.get("lesson", "No lesson available"),
|
| 54 |
+
parsed.get("example", "No example available"),
|
| 55 |
+
parsed.get("real_world_problem", "No real-world problem available")
|
| 56 |
+
)
|
| 57 |
+
except json.JSONDecodeError:
|
| 58 |
+
logging.error(f"Error parsing JSON output: {output}")
|
| 59 |
+
return "Error parsing output", "No example available", "No real-world problem available"
|
| 60 |
|
| 61 |
+
def create_interface() -> gr.Blocks:
|
| 62 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown("# 🎓 Vers3Dynamics Tutor: Your Personal Learning Companion")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column(scale=2):
|
| 67 |
+
subject = gr.Dropdown(
|
| 68 |
+
["Math", "Science", "History", "Literature", "Computer Science"],
|
| 69 |
+
label="Subject",
|
| 70 |
+
info="Choose the subject of your lesson"
|
| 71 |
+
)
|
| 72 |
+
difficulty = gr.Radio(
|
| 73 |
+
["Beginner", "Intermediate", "Advanced"],
|
| 74 |
+
label="Difficulty Level",
|
| 75 |
+
info="Select your proficiency level"
|
| 76 |
+
)
|
| 77 |
+
student_input = gr.Textbox(
|
| 78 |
+
placeholder="Type your topic or question here...",
|
| 79 |
+
label="Your Input",
|
| 80 |
+
info="Enter the topic you want to explore"
|
| 81 |
+
)
|
| 82 |
+
submit_button = gr.Button("📚 Generate Lesson", variant="primary")
|
| 83 |
+
|
| 84 |
+
with gr.Column(scale=3):
|
| 85 |
+
lesson_output = gr.Markdown(label="Lesson")
|
| 86 |
+
example_output = gr.Markdown(label="Example")
|
| 87 |
+
real_world_output = gr.Markdown(label="Real-World Application")
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""
|
| 90 |
+
### How to Use
|
| 91 |
+
1. Select a subject from the dropdown.
|
| 92 |
+
2. Choose your difficulty level.
|
| 93 |
+
3. Enter the topic or question you'd like to explore.
|
| 94 |
+
4. Click 'Generate Lesson' to receive a personalized lesson, example, and real-world application.
|
| 95 |
+
5. Review the AI-generated content to enhance your learning.
|
| 96 |
+
6. Feel free to ask follow-up questions or explore new topics!
|
| 97 |
+
|
| 98 |
+
Remember: This is an AI-powered educational tool. Always verify important information with authoritative sources.
|
| 99 |
+
""")
|
| 100 |
+
|
| 101 |
+
submit_button.click(
|
| 102 |
+
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
|
| 103 |
+
inputs=[subject, difficulty, student_input],
|
| 104 |
+
outputs=[lesson_output, example_output, real_world_output]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return demo
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
demo = create_interface()
|
| 111 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|