Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,36 @@ from groq import Groq
|
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
|
| 7 |
-
# Get Groq API key from environment variables
|
| 8 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
-
if not GROQ_API_KEY:
|
| 10 |
-
raise ValueError("Please set GROQ_API_KEY in the Space settings under 'Variables'.")
|
| 11 |
-
|
| 12 |
# Initialize Groq client
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Function to generate tutor output (lesson, question, feedback)
|
| 16 |
-
def generate_tutor_output(subject, difficulty, student_input,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
prompt = f"""
|
| 18 |
You are an expert tutor in {subject} at the {difficulty} level.
|
| 19 |
The student has provided the following input: "{student_input}"
|
|
@@ -25,27 +45,33 @@ def generate_tutor_output(subject, difficulty, student_input, selected_model):
|
|
| 25 |
|
| 26 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 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
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"role": "user",
|
| 38 |
"content": prompt,
|
| 39 |
}
|
| 40 |
],
|
| 41 |
-
model=
|
| 42 |
-
temperature=0.6,
|
| 43 |
-
top_p=0.95,
|
| 44 |
max_tokens=1000,
|
| 45 |
)
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Inline CSS to match the original styling
|
| 51 |
custom_css = """
|
|
@@ -126,51 +152,47 @@ with gr.Blocks(
|
|
| 126 |
title="AI Tutor with Text and Visuals",
|
| 127 |
css=custom_css
|
| 128 |
) as demo:
|
| 129 |
-
gr.Markdown("# 🎓
|
| 130 |
|
| 131 |
# Section for generating Text-based output
|
| 132 |
with gr.Row():
|
| 133 |
with gr.Column(scale=2):
|
| 134 |
-
# Input fields for subject, difficulty, model, and student input
|
| 135 |
subject = gr.Dropdown(
|
| 136 |
-
["Math", "Science", "History", "
|
| 137 |
-
label="Subject",
|
| 138 |
info="Choose the subject of your lesson"
|
| 139 |
)
|
| 140 |
-
difficulty = gr.
|
| 141 |
-
["Beginner", "Intermediate", "Advanced"],
|
| 142 |
-
label="Difficulty Level",
|
| 143 |
-
info="Select your
|
| 144 |
)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
label="
|
| 148 |
-
value="qwen-
|
| 149 |
-
info="
|
| 150 |
)
|
| 151 |
student_input = gr.Textbox(
|
| 152 |
-
placeholder="Type your query here...",
|
| 153 |
-
label="Your Input",
|
| 154 |
info="Enter the topic you want to learn"
|
| 155 |
)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
with gr.Column(scale=3):
|
| 159 |
-
# Output fields for lesson, question, and feedback
|
| 160 |
lesson_output = gr.Markdown(label="Lesson", elem_classes="markdown-output")
|
| 161 |
question_output = gr.Markdown(label="Comprehension Question", elem_classes="markdown-output")
|
| 162 |
feedback_output = gr.Markdown(label="Feedback", elem_classes="markdown-output")
|
| 163 |
|
| 164 |
-
# Section for Visual output using iframe
|
| 165 |
with gr.Row():
|
| 166 |
with gr.Column(scale=2):
|
| 167 |
-
# Note: We're using an iframe, so we don't need a model selector for image generation
|
| 168 |
gr.Markdown("## Image Generation")
|
| 169 |
gr.Markdown("Use the embedded tool below to generate images.")
|
| 170 |
submit_button_visual = gr.Button("Open Visual Tool", variant="primary")
|
| 171 |
|
| 172 |
with gr.Column(scale=3):
|
| 173 |
-
# Output field for the iframe
|
| 174 |
visual_output = gr.HTML(label="Image Generation Tool")
|
| 175 |
|
| 176 |
gr.HTML("""
|
|
@@ -181,20 +203,21 @@ with gr.Blocks(
|
|
| 181 |
|
| 182 |
gr.Markdown("""
|
| 183 |
### How to Use
|
| 184 |
-
1. **Text Section**: Select a subject, difficulty, and model, type your query, and click 'Generate Lesson
|
| 185 |
2. **Visual Section**: Click 'Open Visual Tool' to load the image generation tool, then use it to generate images.
|
| 186 |
3. Review the AI-generated content to enhance your learning experience!
|
| 187 |
|
| 188 |
-
*Example*: Try "Explain the
|
| 189 |
""")
|
| 190 |
|
| 191 |
-
def
|
|
|
|
| 192 |
try:
|
| 193 |
-
|
| 194 |
-
parsed = json.loads(tutor_output)
|
| 195 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 196 |
except Exception as e:
|
| 197 |
-
|
|
|
|
| 198 |
|
| 199 |
def load_visual_tool():
|
| 200 |
return """
|
|
@@ -202,9 +225,9 @@ with gr.Blocks(
|
|
| 202 |
"""
|
| 203 |
|
| 204 |
# Generate Text-based Output
|
| 205 |
-
|
| 206 |
-
fn=
|
| 207 |
-
inputs=[subject, difficulty, student_input,
|
| 208 |
outputs=[lesson_output, question_output, feedback_output]
|
| 209 |
)
|
| 210 |
|
|
@@ -215,4 +238,5 @@ with gr.Blocks(
|
|
| 215 |
outputs=[visual_output]
|
| 216 |
)
|
| 217 |
|
| 218 |
-
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Initialize Groq client
|
| 8 |
+
try:
|
| 9 |
+
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
|
| 10 |
+
print("API Key:", GROQ_API_KEY) # Debug print
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
except KeyError as e:
|
| 13 |
+
raise ValueError("GROQ_API_KEY not found in environment variables. Please set it in the Space settings under 'Variables'.")
|
| 14 |
+
|
| 15 |
+
# Define valid models (only the two specified models)
|
| 16 |
+
valid_models = [
|
| 17 |
+
"qwen-2.5-32b",
|
| 18 |
+
"qwen-2.5-coder-32b"
|
| 19 |
+
]
|
| 20 |
|
| 21 |
# Function to generate tutor output (lesson, question, feedback)
|
| 22 |
+
def generate_tutor_output(subject, difficulty, student_input, model):
|
| 23 |
+
if not subject or not difficulty or not student_input:
|
| 24 |
+
return json.dumps({
|
| 25 |
+
"lesson": "Error: Please fill in all fields (subject, difficulty, and input).",
|
| 26 |
+
"question": "No question available",
|
| 27 |
+
"feedback": "No feedback available"
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
if model not in valid_models:
|
| 31 |
+
return json.dumps({
|
| 32 |
+
"lesson": f"Error: Invalid model selected: {model}. Please choose a valid model.",
|
| 33 |
+
"question": "No question available",
|
| 34 |
+
"feedback": "No feedback available"
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
prompt = f"""
|
| 38 |
You are an expert tutor in {subject} at the {difficulty} level.
|
| 39 |
The student has provided the following input: "{student_input}"
|
|
|
|
| 45 |
|
| 46 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 47 |
"""
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
print(f"Calling Groq API with model: {model}, subject: {subject}, difficulty: {difficulty}, input: {student_input}")
|
| 51 |
completion = client.chat.completions.create(
|
| 52 |
messages=[
|
| 53 |
{
|
| 54 |
"role": "system",
|
| 55 |
+
"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 with examples suitable for {difficulty} level students. 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.",
|
| 56 |
},
|
| 57 |
{
|
| 58 |
"role": "user",
|
| 59 |
"content": prompt,
|
| 60 |
}
|
| 61 |
],
|
| 62 |
+
model=model,
|
|
|
|
|
|
|
| 63 |
max_tokens=1000,
|
| 64 |
)
|
| 65 |
+
response = completion.choices[0].message.content
|
| 66 |
+
print(f"Groq API Response: {response}") # Debug print
|
| 67 |
+
return response
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"Groq API Error: {str(e)}")
|
| 70 |
+
return json.dumps({
|
| 71 |
+
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
|
| 72 |
+
"question": "No question available",
|
| 73 |
+
"feedback": "No feedback available due to API error"
|
| 74 |
+
})
|
| 75 |
|
| 76 |
# Inline CSS to match the original styling
|
| 77 |
custom_css = """
|
|
|
|
| 152 |
title="AI Tutor with Text and Visuals",
|
| 153 |
css=custom_css
|
| 154 |
) as demo:
|
| 155 |
+
gr.Markdown("# 🎓 Learn & Explore")
|
| 156 |
|
| 157 |
# Section for generating Text-based output
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=2):
|
|
|
|
| 160 |
subject = gr.Dropdown(
|
| 161 |
+
["Math", "Science", "History", "Geography", "Economics"],
|
| 162 |
+
label="Subject",
|
| 163 |
info="Choose the subject of your lesson"
|
| 164 |
)
|
| 165 |
+
difficulty = gr.Dropdown(
|
| 166 |
+
["Beginner", "Intermediate", "Advanced"],
|
| 167 |
+
label="Difficulty Level",
|
| 168 |
+
info="Select your difficulty level"
|
| 169 |
)
|
| 170 |
+
model_select = gr.Dropdown(
|
| 171 |
+
valid_models,
|
| 172 |
+
label="AI Model",
|
| 173 |
+
value="qwen-2.5-32b",
|
| 174 |
+
info="Select the AI model to use"
|
| 175 |
)
|
| 176 |
student_input = gr.Textbox(
|
| 177 |
+
placeholder="Type your query here...",
|
| 178 |
+
label="Your Input",
|
| 179 |
info="Enter the topic you want to learn"
|
| 180 |
)
|
| 181 |
+
submit_button = gr.Button("Generate Lesson and Question", variant="primary")
|
| 182 |
+
|
| 183 |
with gr.Column(scale=3):
|
|
|
|
| 184 |
lesson_output = gr.Markdown(label="Lesson", elem_classes="markdown-output")
|
| 185 |
question_output = gr.Markdown(label="Comprehension Question", elem_classes="markdown-output")
|
| 186 |
feedback_output = gr.Markdown(label="Feedback", elem_classes="markdown-output")
|
| 187 |
|
| 188 |
+
# Section for Visual output using iframe (unchanged)
|
| 189 |
with gr.Row():
|
| 190 |
with gr.Column(scale=2):
|
|
|
|
| 191 |
gr.Markdown("## Image Generation")
|
| 192 |
gr.Markdown("Use the embedded tool below to generate images.")
|
| 193 |
submit_button_visual = gr.Button("Open Visual Tool", variant="primary")
|
| 194 |
|
| 195 |
with gr.Column(scale=3):
|
|
|
|
| 196 |
visual_output = gr.HTML(label="Image Generation Tool")
|
| 197 |
|
| 198 |
gr.HTML("""
|
|
|
|
| 203 |
|
| 204 |
gr.Markdown("""
|
| 205 |
### How to Use
|
| 206 |
+
1. **Text Section**: Select a subject, difficulty level, and model, type your query, and click 'Generate Lesson and Question' to get your personalized lesson, question, and feedback.
|
| 207 |
2. **Visual Section**: Click 'Open Visual Tool' to load the image generation tool, then use it to generate images.
|
| 208 |
3. Review the AI-generated content to enhance your learning experience!
|
| 209 |
|
| 210 |
+
*Example*: Try "Explain the water cycle" for a Beginner Science lesson, and use the image tool to generate "a photo of a rainforest ecosystem".
|
| 211 |
""")
|
| 212 |
|
| 213 |
+
def process_output(output):
|
| 214 |
+
print(f"Raw API Output: {output}") # Debug print
|
| 215 |
try:
|
| 216 |
+
parsed = json.loads(output)
|
|
|
|
| 217 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 218 |
except Exception as e:
|
| 219 |
+
print(f"JSON Parsing Error: {str(e)}")
|
| 220 |
+
return "Error parsing output", "No question available", "No feedback available"
|
| 221 |
|
| 222 |
def load_visual_tool():
|
| 223 |
return """
|
|
|
|
| 225 |
"""
|
| 226 |
|
| 227 |
# Generate Text-based Output
|
| 228 |
+
submit_button.click(
|
| 229 |
+
fn=lambda s, d, i, m: process_output(generate_tutor_output(s, d, i, m)),
|
| 230 |
+
inputs=[subject, difficulty, student_input, model_select],
|
| 231 |
outputs=[lesson_output, question_output, feedback_output]
|
| 232 |
)
|
| 233 |
|
|
|
|
| 238 |
outputs=[visual_output]
|
| 239 |
)
|
| 240 |
|
| 241 |
+
if __name__ == "__main__":
|
| 242 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|