Spaces:
Running
Running
Updated with functionality for flash cards.
#1
by
manasic
- opened
app.py
CHANGED
@@ -158,5 +158,75 @@ async def get_strong_weak_topics(email: str):
|
|
158 |
else:
|
159 |
return JSONResponse(content={"error": "No test results found for this email"})
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
if __name__ == "__main__":
|
162 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
158 |
else:
|
159 |
return JSONResponse(content={"error": "No test results found for this email"})
|
160 |
|
161 |
+
@app.post("/generate_flashcards")
|
162 |
+
async def generate_flashcards(email: str):
|
163 |
+
df = generate_df()
|
164 |
+
df_email = df[df['email'] == email]
|
165 |
+
|
166 |
+
if len(df_email) < 10:
|
167 |
+
return JSONResponse(content={"message": "Please attempt at least 10 tests to enable flashcard generation."})
|
168 |
+
|
169 |
+
# Step 1: Get the weak topics via DeepSeek
|
170 |
+
response = df_email['responses'].values[:10]
|
171 |
+
formatted_data = str(response)
|
172 |
+
|
173 |
+
schema = {
|
174 |
+
'weak_topics': ['Topic#1', 'Topic#2', '...'],
|
175 |
+
'strong_topics': ['Topic#1', 'Topic#2', '...']
|
176 |
+
}
|
177 |
+
|
178 |
+
completion = client.chat.completions.create(
|
179 |
+
model="deepseek-chat",
|
180 |
+
response_format={"type": "json_object"},
|
181 |
+
messages=[
|
182 |
+
{
|
183 |
+
"role": "system",
|
184 |
+
"content": f"""You are an Educational Performance Analyst focusing on student performance.
|
185 |
+
Analyze the provided student responses to identify and categorize topics into 'weak' and 'strong' based on their performance.
|
186 |
+
Do not add any explanations - return ONLY valid JSON."""
|
187 |
+
},
|
188 |
+
{
|
189 |
+
"role": "user",
|
190 |
+
"content": f"""
|
191 |
+
Here is the raw data:
|
192 |
+
{formatted_data}
|
193 |
+
|
194 |
+
Convert this data into JSON that matches this schema:
|
195 |
+
{json.dumps(schema, indent=2)}
|
196 |
+
"""
|
197 |
+
}
|
198 |
+
],
|
199 |
+
temperature=0.0
|
200 |
+
)
|
201 |
+
|
202 |
+
# Extract weak topics
|
203 |
+
strong_weak_json = json.loads(completion.choices[0].message.content)
|
204 |
+
weak_topics = strong_weak_json.get("weak_topics", [])
|
205 |
+
|
206 |
+
if not weak_topics:
|
207 |
+
return JSONResponse(content={"message": "Could not extract weak topics."})
|
208 |
+
|
209 |
+
# Step 2: Generate flashcards using Gemini
|
210 |
+
topic_str = ", ".join(weak_topics)
|
211 |
+
flashcard_prompt = f"""Create 5 concise, simple, straightforward and distinct Anki cards to study the following topic, each with a front and back.
|
212 |
+
Avoid repeating the content in the front on the back of the card. Avoid explicitly referring to the author or the article.
|
213 |
+
Use the following format:
|
214 |
+
Front: [front section of card 1]
|
215 |
+
Back: [back section of card 1]
|
216 |
+
...
|
217 |
+
The topics: {topic_str}
|
218 |
+
"""
|
219 |
+
|
220 |
+
flashcard_response = model.generate_content(flashcard_prompt)
|
221 |
+
|
222 |
+
# Step 3: Parse Gemini response into JSON format
|
223 |
+
flashcards_raw = flashcard_response.text.strip()
|
224 |
+
flashcard_pattern = re.findall(r"Front:\s*(.*?)\nBack:\s*(.*?)(?=\nFront:|\Z)", flashcards_raw, re.DOTALL)
|
225 |
+
|
226 |
+
flashcards = [{"Front": front.strip(), "Back": back.strip()} for front, back in flashcard_pattern]
|
227 |
+
|
228 |
+
return JSONResponse(content=flashcards)
|
229 |
+
|
230 |
+
|
231 |
if __name__ == "__main__":
|
232 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|