SameerArz commited on
Commit
e9e10d6
ยท
verified ยท
1 Parent(s): de55525

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -19
app.py CHANGED
@@ -5,6 +5,7 @@ import json
5
 
6
  # Initialize Groq client
7
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
 
8
 
9
  def generate_tutor_output(subject, grade, student_input, model):
10
  prompt = f"""
@@ -19,22 +20,31 @@ def generate_tutor_output(subject, grade, student_input, model):
19
  Format your response as a JSON object with keys: "lesson", "question", "options", "correct_answer", "feedback"
20
  """
21
 
22
- completion = client.chat.completions.create(
23
- messages=[
24
- {
25
- "role": "system",
26
- "content": f"You are a fun, creative AI tutor for {grade} graders, expert in {subject}. You explain concepts in a simple, exciting way with relatable examples (like math problems for their age). Your goal is to spark curiosity and help students practice what they learn!",
27
- },
28
- {
29
- "role": "user",
30
- "content": prompt,
31
- }
32
- ],
33
- model=model,
34
- max_tokens=1200,
35
- )
36
-
37
- return completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
38
 
39
  def check_answer(selected_answer, correct_answer):
40
  if selected_answer == correct_answer:
@@ -61,7 +71,7 @@ with gr.Blocks(title="Learn & Practice ๐Ÿš€") as demo:
61
  model_select = gr.Dropdown(
62
  [
63
  "mixtral-8x7b-32768",
64
- "qwen-2.5-coder-32b",
65
  "qwen-2.5-32b"
66
  ],
67
  label="AI Tutor Model",
@@ -95,6 +105,7 @@ with gr.Blocks(title="Learn & Practice ๐Ÿš€") as demo:
95
  """)
96
 
97
  def process_output(output):
 
98
  try:
99
  parsed = json.loads(output)
100
  options = [f"{k}. {v}" for k, v in zip(["a", "b", "c", "d"], parsed["options"])]
@@ -105,9 +116,10 @@ with gr.Blocks(title="Learn & Practice ๐Ÿš€") as demo:
105
  parsed["correct_answer"],
106
  parsed["feedback"]
107
  )
108
- except:
 
109
  return (
110
- "Error generating lesson",
111
  "No question available",
112
  [],
113
  "",
@@ -115,6 +127,7 @@ with gr.Blocks(title="Learn & Practice ๐Ÿš€") as demo:
115
  )
116
 
117
  def update_interface(subject, grade, student_input, model):
 
118
  output = generate_tutor_output(subject, grade, student_input, model)
119
  lesson, question, options, correct_answer, feedback = process_output(output)
120
  return (
 
5
 
6
  # Initialize Groq client
7
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
8
+ print("API Key:", os.environ.get("GROQ_API_KEY")) # Debug print
9
 
10
  def generate_tutor_output(subject, grade, student_input, model):
11
  prompt = f"""
 
20
  Format your response as a JSON object with keys: "lesson", "question", "options", "correct_answer", "feedback"
21
  """
22
 
23
+ try:
24
+ completion = client.chat.completions.create(
25
+ messages=[
26
+ {
27
+ "role": "system",
28
+ "content": f"You are a fun, creative AI tutor for {grade} graders, expert in {subject}. You explain concepts in a simple, exciting way with relatable examples (like math problems for their age). Your goal is to spark curiosity and help students practice what they learn!",
29
+ },
30
+ {
31
+ "role": "user",
32
+ "content": prompt,
33
+ }
34
+ ],
35
+ model=model,
36
+ max_tokens=1200,
37
+ )
38
+ return completion.choices[0].message.content
39
+ except Exception as e:
40
+ print(f"Groq API Error: {str(e)}") # Debug print
41
+ return json.dumps({
42
+ "lesson": f"Error: Could not generate lesson. API error: {str(e)}",
43
+ "question": "No question available",
44
+ "options": [],
45
+ "correct_answer": "",
46
+ "feedback": "No feedback available due to API error"
47
+ })
48
 
49
  def check_answer(selected_answer, correct_answer):
50
  if selected_answer == correct_answer:
 
71
  model_select = gr.Dropdown(
72
  [
73
  "mixtral-8x7b-32768",
74
+ "llama3-8b-8192", # Fallback model
75
  "qwen-2.5-32b"
76
  ],
77
  label="AI Tutor Model",
 
105
  """)
106
 
107
  def process_output(output):
108
+ print(f"Raw API Output: {output}") # Debug print
109
  try:
110
  parsed = json.loads(output)
111
  options = [f"{k}. {v}" for k, v in zip(["a", "b", "c", "d"], parsed["options"])]
 
116
  parsed["correct_answer"],
117
  parsed["feedback"]
118
  )
119
+ except Exception as e:
120
+ print(f"JSON Parsing Error: {str(e)}") # Debug print
121
  return (
122
+ f"Error parsing response: {str(e)}",
123
  "No question available",
124
  [],
125
  "",
 
127
  )
128
 
129
  def update_interface(subject, grade, student_input, model):
130
+ print(f"Selected Model: {model}") # Debug print
131
  output = generate_tutor_output(subject, grade, student_input, model)
132
  lesson, question, options, correct_answer, feedback = process_output(output)
133
  return (