Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -5,25 +5,24 @@ import os
|
|
| 5 |
# Initialize Groq client
|
| 6 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 7 |
|
| 8 |
-
def generate_tutor_output(
|
| 9 |
prompt = f"""
|
| 10 |
-
You are an expert
|
| 11 |
-
The student has
|
| 12 |
|
| 13 |
-
Please generate:
|
| 14 |
-
1.
|
| 15 |
-
2.
|
| 16 |
-
3.
|
| 17 |
-
4. A short test based on the chapter content
|
| 18 |
|
| 19 |
-
Format your response as a JSON object with keys: "
|
| 20 |
"""
|
| 21 |
|
| 22 |
completion = client.chat.completions.create(
|
| 23 |
messages=[
|
| 24 |
{
|
| 25 |
"role": "system",
|
| 26 |
-
"content": "You are the world's best AI tutor
|
| 27 |
},
|
| 28 |
{
|
| 29 |
"role": "user",
|
|
@@ -34,22 +33,18 @@ def generate_tutor_output(subject, difficulty, chapter, student_input):
|
|
| 34 |
max_tokens=1000,
|
| 35 |
)
|
| 36 |
|
|
|
|
| 37 |
return completion.choices[0].message.content
|
| 38 |
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
-
gr.Markdown("# 🎓
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column(scale=2):
|
| 44 |
-
subject = gr.Dropdown(
|
| 45 |
-
["Physics", "Math", "Science", "History", "Literature", "Code", "AI"],
|
| 46 |
-
label="Subject",
|
| 47 |
-
info="Choose the subject of your lesson"
|
| 48 |
-
)
|
| 49 |
chapter = gr.Dropdown(
|
| 50 |
["Chapter 1: Kinematics", "Chapter 2: Dynamics", "Chapter 3: Energy", "Chapter 4: Waves"],
|
| 51 |
-
label="Chapter",
|
| 52 |
-
info="
|
| 53 |
)
|
| 54 |
difficulty = gr.Radio(
|
| 55 |
["Beginner", "Intermediate", "Advanced"],
|
|
@@ -57,41 +52,59 @@ with gr.Blocks() as demo:
|
|
| 57 |
info="Select your proficiency level"
|
| 58 |
)
|
| 59 |
student_input = gr.Textbox(
|
| 60 |
-
placeholder="
|
| 61 |
label="Your Input",
|
| 62 |
-
info="
|
| 63 |
)
|
| 64 |
-
submit_button = gr.Button("Generate
|
| 65 |
|
| 66 |
with gr.Column(scale=3):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
gr.Markdown("""
|
| 73 |
-
###
|
| 74 |
-
1. Select a
|
| 75 |
-
2. Choose
|
| 76 |
-
3.
|
| 77 |
-
4.
|
| 78 |
-
5. Click 'Generate
|
| 79 |
-
6. Review the AI-generated lesson, question, feedback, and test.
|
| 80 |
-
7. Feel free to ask follow-up questions or explore new topics!
|
| 81 |
""")
|
| 82 |
|
| 83 |
-
def process_output(output):
|
| 84 |
try:
|
|
|
|
| 85 |
parsed = eval(output)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
submit_button.click(
|
| 91 |
-
fn=lambda
|
| 92 |
-
inputs=[
|
| 93 |
-
outputs=[
|
| 94 |
)
|
| 95 |
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
# Initialize Groq client
|
| 6 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 7 |
|
| 8 |
+
def generate_tutor_output(chapter, difficulty, student_input):
|
| 9 |
prompt = f"""
|
| 10 |
+
You are an expert 10th-grade physics tutor at the {difficulty} level.
|
| 11 |
+
The student has selected the following chapter: "{chapter}" and provided this input: "{student_input}"
|
| 12 |
|
| 13 |
+
Please generate an unsolved test with:
|
| 14 |
+
1. Multiple Choice Questions (MCQs)
|
| 15 |
+
2. Short Answer Questions
|
| 16 |
+
3. Long Answer Questions
|
|
|
|
| 17 |
|
| 18 |
+
Format your response as a JSON object with keys: "mcqs", "short_questions", "long_questions"
|
| 19 |
"""
|
| 20 |
|
| 21 |
completion = client.chat.completions.create(
|
| 22 |
messages=[
|
| 23 |
{
|
| 24 |
"role": "system",
|
| 25 |
+
"content": "You are the world's best AI tutor specializing in 10th-grade physics, adept at creating customized tests. Generate MCQs, short questions, and long questions based on the provided chapter."
|
| 26 |
},
|
| 27 |
{
|
| 28 |
"role": "user",
|
|
|
|
| 33 |
max_tokens=1000,
|
| 34 |
)
|
| 35 |
|
| 36 |
+
print("Raw Model Output:", completion.choices[0].message.content) # Debugging output
|
| 37 |
return completion.choices[0].message.content
|
| 38 |
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# 🎓 10th Grade Physics Tutor - Test Generator")
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
chapter = gr.Dropdown(
|
| 45 |
["Chapter 1: Kinematics", "Chapter 2: Dynamics", "Chapter 3: Energy", "Chapter 4: Waves"],
|
| 46 |
+
label="Select Chapter",
|
| 47 |
+
info="Choose the chapter for the test"
|
| 48 |
)
|
| 49 |
difficulty = gr.Radio(
|
| 50 |
["Beginner", "Intermediate", "Advanced"],
|
|
|
|
| 52 |
info="Select your proficiency level"
|
| 53 |
)
|
| 54 |
student_input = gr.Textbox(
|
| 55 |
+
placeholder="Enter additional details...",
|
| 56 |
label="Your Input",
|
| 57 |
+
info="Specify 'MCQs', 'short questions', or 'long questions' if needed"
|
| 58 |
)
|
| 59 |
+
submit_button = gr.Button("Generate Test", variant="primary")
|
| 60 |
|
| 61 |
with gr.Column(scale=3):
|
| 62 |
+
mcq_output = gr.Markdown(label="MCQs")
|
| 63 |
+
short_question_output = gr.Markdown(label="Short Questions")
|
| 64 |
+
long_question_output = gr.Markdown(label="Long Questions")
|
| 65 |
+
|
|
|
|
| 66 |
gr.Markdown("""
|
| 67 |
+
### Instructions
|
| 68 |
+
1. Select a chapter to generate questions from.
|
| 69 |
+
2. Choose your difficulty level.
|
| 70 |
+
3. Provide any additional details if needed.
|
| 71 |
+
4. Specify 'MCQs', 'short questions', or 'long questions' to filter the output, or leave blank for all types.
|
| 72 |
+
5. Click 'Generate Test' to receive questions based on the chapter.
|
|
|
|
|
|
|
| 73 |
""")
|
| 74 |
|
| 75 |
+
def process_output(output, user_input):
|
| 76 |
try:
|
| 77 |
+
# Parse the JSON output
|
| 78 |
parsed = eval(output)
|
| 79 |
+
|
| 80 |
+
# Filter based on the user input
|
| 81 |
+
mcqs, short_questions, long_questions = "", "", ""
|
| 82 |
+
|
| 83 |
+
if "mcq" in user_input.lower():
|
| 84 |
+
mcqs = "\n".join([f"**Q:** {mcq['question']} - Options: {', '.join(mcq['options'])}" for mcq in parsed.get("mcqs", [])])
|
| 85 |
+
|
| 86 |
+
if "short" in user_input.lower():
|
| 87 |
+
short_questions = "\n".join([sq["question"] for sq in parsed.get("short_questions", [])])
|
| 88 |
+
|
| 89 |
+
if "long" in user_input.lower():
|
| 90 |
+
long_questions = "\n".join([lq["question"] for lq in parsed.get("long_questions", [])])
|
| 91 |
+
|
| 92 |
+
# Default to all types if no specific type is mentioned
|
| 93 |
+
if not any(keyword in user_input.lower() for keyword in ["mcq", "short", "long"]):
|
| 94 |
+
mcqs = "\n".join([f"**Q:** {mcq['question']} - Options: {', '.join(mcq['options'])}" for mcq in parsed.get("mcqs", [])])
|
| 95 |
+
short_questions = "\n".join([sq["question"] for sq in parsed.get("short_questions", [])])
|
| 96 |
+
long_questions = "\n".join([lq["question"] for lq in parsed.get("long_questions", [])])
|
| 97 |
+
|
| 98 |
+
return mcqs or "No MCQs available", short_questions or "No short questions available", long_questions or "No long questions available"
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print("Error processing output:", e) # Additional error message for debugging
|
| 102 |
+
return "Error parsing output", "No short questions available", "No long questions available"
|
| 103 |
|
| 104 |
submit_button.click(
|
| 105 |
+
fn=lambda c, d, i: process_output(generate_tutor_output(c, d, i), i),
|
| 106 |
+
inputs=[chapter, difficulty, student_input],
|
| 107 |
+
outputs=[mcq_output, short_question_output, long_question_output]
|
| 108 |
)
|
| 109 |
|
| 110 |
+
if __name__ == "__main__":
|
|
|