Spaces:
Sleeping
Sleeping
Update app.py
#2
by
manasic
- opened
app.py
CHANGED
|
@@ -227,6 +227,94 @@ The topics: {topic_str}
|
|
| 227 |
|
| 228 |
return JSONResponse(content=flashcards)
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
| 232 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 227 |
|
| 228 |
return JSONResponse(content=flashcards)
|
| 229 |
|
| 230 |
+
@app.post("/generate_detailed_summary")
|
| 231 |
+
async def generate_detailed_summary(email: str):
|
| 232 |
+
df = generate_df()
|
| 233 |
+
df_email = df[df['email'] == email]
|
| 234 |
+
|
| 235 |
+
if len(df_email) < 10:
|
| 236 |
+
return JSONResponse(content={"message": "Please attempt at least 10 tests to enable detailed summary generation."})
|
| 237 |
+
|
| 238 |
+
# Step 1: Get the weak topics via DeepSeek
|
| 239 |
+
response = df_email['responses'].values[:10]
|
| 240 |
+
formatted_data = str(response)
|
| 241 |
+
|
| 242 |
+
schema = {
|
| 243 |
+
'weak_topics': ['Topic#1', 'Topic#2', '...'],
|
| 244 |
+
'strong_topics': ['Topic#1', 'Topic#2', '...']
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
completion = client.chat.completions.create(
|
| 248 |
+
model="deepseek-chat",
|
| 249 |
+
response_format={"type": "json_object"},
|
| 250 |
+
messages=[
|
| 251 |
+
{
|
| 252 |
+
"role": "system",
|
| 253 |
+
"content": f"""You are an Educational Performance Analyst focusing on student performance.
|
| 254 |
+
Analyze the provided student responses to identify and categorize topics into 'weak' and 'strong' based on their performance.
|
| 255 |
+
Do not add any explanations - return ONLY valid JSON."""
|
| 256 |
+
},
|
| 257 |
+
{
|
| 258 |
+
"role": "user",
|
| 259 |
+
"content": f"""
|
| 260 |
+
Here is the raw data:
|
| 261 |
+
{formatted_data}
|
| 262 |
+
|
| 263 |
+
Convert this data into JSON that matches this schema:
|
| 264 |
+
{json.dumps(schema, indent=2)}
|
| 265 |
+
"""
|
| 266 |
+
}
|
| 267 |
+
],
|
| 268 |
+
temperature=0.0
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
# Extract weak topics
|
| 272 |
+
strong_weak_json = json.loads(completion.choices[0].message.content)
|
| 273 |
+
weak_topics = strong_weak_json.get("weak_topics", [])
|
| 274 |
+
|
| 275 |
+
if not weak_topics:
|
| 276 |
+
return JSONResponse(content={"message": "Could not extract weak topics."})
|
| 277 |
+
|
| 278 |
+
# Step 2: Generate flashcards using Gemini
|
| 279 |
+
topic_str = ", ".join(weak_topics)
|
| 280 |
+
# flashcard_prompt = f"""Create 5 concise, simple, straightforward and distinct Anki cards to study the following topic, each with a front and back.
|
| 281 |
+
# Avoid repeating the content in the front on the back of the card. Avoid explicitly referring to the author or the article.
|
| 282 |
+
# Use the following format:
|
| 283 |
+
# Front: [front section of card 1]
|
| 284 |
+
# Back: [back section of card 1]
|
| 285 |
+
# ...
|
| 286 |
+
# The topics: {topic_str}
|
| 287 |
+
# """
|
| 288 |
+
|
| 289 |
+
# flashcard_response = model.generate_content(flashcard_prompt)
|
| 290 |
+
|
| 291 |
+
# # Step 3: Parse Gemini response into JSON format
|
| 292 |
+
# flashcards_raw = flashcard_response.text.strip()
|
| 293 |
+
# flashcard_pattern = re.findall(r"Front:\s*(.*?)\nBack:\s*(.*?)(?=\nFront:|\Z)", flashcards_raw, re.DOTALL)
|
| 294 |
+
|
| 295 |
+
# flashcards = [{"Front": front.strip(), "Back": back.strip()} for front, back in flashcard_pattern]
|
| 296 |
+
|
| 297 |
+
summarization_prompt = f"""
|
| 298 |
+
Write an informative and concise summary (approximately 200 words) for each of the following topics.
|
| 299 |
+
Do not mention the author or source. Use clear and academic language.
|
| 300 |
+
|
| 301 |
+
List of topics:
|
| 302 |
+
{topic_str}
|
| 303 |
+
|
| 304 |
+
Use the following format:
|
| 305 |
+
Topic: [topic name]
|
| 306 |
+
Summary: [200-word summary]
|
| 307 |
+
"""
|
| 308 |
+
|
| 309 |
+
summary_response = model.generate_content(summarization_prompt)
|
| 310 |
+
|
| 311 |
+
# Step 3: Parse response into JSON format
|
| 312 |
+
summary_raw = summary_response.text.strip()
|
| 313 |
+
summary_pattern = re.findall(r"Topic:\s*(.*?)\nSummary:\s*(.*?)(?=\nTopic:|\Z)", summary_raw, re.DOTALL)
|
| 314 |
+
|
| 315 |
+
summaries = [{topic.strip(): summary.strip() for topic, summary in summary_pattern}]
|
| 316 |
+
|
| 317 |
+
return JSONResponse(content=summaries)
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
| 320 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|