Spaces:
Sleeping
Sleeping
File size: 15,898 Bytes
cc0a162 c793218 161cc5a c793218 161cc5a c793218 cc0a162 c793218 cc0a162 c793218 b0c010e cc0a162 b0c010e c793218 cc0a162 f61c88d 4b8c16b c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 c793218 cc0a162 f22b2b6 cc0a162 c793218 cc0a162 f22b2b6 cc0a162 c793218 cc0a162 161cc5a cc0a162 161cc5a cc0a162 161cc5a c793218 161cc5a c793218 161cc5a c793218 cc0a162 c793218 7cb07ee cc0a162 7cb07ee cc0a162 7cb07ee cc0a162 7cb07ee cc0a162 7cb07ee d17ee75 cc0a162 d17ee75 7cb07ee c793218 bf9121d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# Import necessary libraries from FastAPI for creating the API, handling uploads, forms, and HTTP exceptions
from fastapi import FastAPI, UploadFile, Form, HTTPException
from pydantic import BaseModel
import uvicorn
from fastapi.responses import JSONResponse
from typing import Dict
import hashlib
from openai import OpenAI
from dotenv import load_dotenv
from fastapi.middleware.cors import CORSMiddleware
from firebase_admin import firestore
import json
import re
import pandas as pd
import google.generativeai as genai
from google.generativeai import GenerativeModel
import os
load_dotenv()
client = OpenAI(api_key=os.getenv('DEEPSEEK_API_KEY'), base_url="https://api.deepseek.com",)
# Initialize Gemini LLM
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model = genai.GenerativeModel("gemini-2.0-flash")
# Firebase Admin SDK Initialization
# Get the Firebase credentials (as a JSON string) from environment variables
import firebase_admin
from firebase_admin import credentials
cred_dic = os.getenv("Firebase_cred")
# Parse the JSON string into a Python dictionary
cred_dict = json.loads(cred_dic)
# Initialize the Firebase Admin app with the credentials
# This needs to be done only once
cred = credentials.Certificate(cred_dict)
firebase_admin.initialize_app(cred)
# Create an instance of the FastAPI application
app = FastAPI()
# Add CORSMiddleware to the application
# This allows requests from any origin ("*"), supports credentials,
# allows all HTTP methods ("*"), and allows all headers ("*").
# Be cautious with "*" in production environments; specify origins if possible.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def generate_df():
data = [] # Initialize an empty list to store document data
db = firestore.client() # Get a Firestore client instance
docs = db.collection("test_results").get()# Retrieve all documents from the "test_results" collection
for doc in docs:
doc_data = doc.to_dict() # Convert Firestore document to a dictionary
doc_data['id'] = doc.id # Add the document ID to the dictionary
data.append(doc_data) # Append the document data to the list
df = pd.DataFrame(data)
return df
def generate_feedback(email, test_id):
"""
Generates feedback for a specific test taken by a student.
It filters the test results by email and test_id, then uses the Gemini model
to generate constructive feedback based on the student's responses for that test.
Args:
email (str): The email of the student.
test_id (str): The ID of the test.
Returns:
str: The generated feedback text, or None if no test results are found.
"""
df = generate_df()
df_email = df[df['email'] == email] # Get the DataFrame of all test results
df_test_id = df_email[df_email['id'] == test_id] # Filter by student's email
if not df_test_id.empty: # Check if any matching test result was found
response = df_test_id['responses'].values[0] # Get the 'responses' field from the first (and only) matching row
# Prepare the prompt for the Gemini model
feedback = model.generate_content(f"""You are an experienced tutor analyzing a student's test responses to provide constructive feedback. Below is the student's test history in JSON format. Your task is to:
Identify Strengths: Highlight areas where the student performed well, demonstrating a strong understanding of the concepts.
Identify Weaknesses: Point out areas where the student struggled or made consistent errors, indicating gaps in understanding.
Provide Actionable Suggestions: Offer specific advice on how the student can improve their performance in future tests.
Encourage and Motivate: End with positive reinforcement to keep the student motivated.
Test History:{str(response)} """)
return feedback.text # Return the text part of the response
else:
print("No test results found for this id")
def generate_overall_feedback(email):
"""
Generates overall feedback for a student based on all their test results.
It filters test results by email and uses the Gemini model to provide
a holistic view of the student's performance.
Args:
email (str): The email of the student.
Returns:
str: The generated overall feedback text, or None if no test results are found.
"""
df = generate_df()
df_email = df[df['email'] == email]
if not df_email.empty:
response = df_email['responses'].values
feedback = model.generate_content(f"""You are an experienced tutor analyzing a student's test responses to provide constructive feedback. Below is the student's test history in list format. Your task is to:
Identify Strengths: Highlight areas where the student performed well, demonstrating a strong understanding of the concepts.
Identify Weaknesses: Point out areas where the student struggled or made consistent errors, indicating gaps in understanding.
Provide Actionable Suggestions: Offer specific advice on how the student can improve their performance in future tests.
Encourage and Motivate: End with positive reinforcement to keep the student motivated.
Test History:{str(response)} """)
return feedback.text
else:
print("Please try again with a valid email")
@app.post("/get_single_feedback")
async def get_single_feedback(email: str, test_id: str):
"""
API endpoint to get feedback for a single test.
Expects 'email' and 'test_id' as form data.
"""
feedback = generate_feedback(email, test_id)
return JSONResponse(content={"feedback": feedback})
@app.post("/get_overall_feedback")
async def get_overall_feedback(email: str):
# """
# API endpoint to get overall feedback for a student.
# Expects 'email' as form data.
# """
feedback = generate_overall_feedback(email) # Call the helper function to generate overall feedback
return JSONResponse(content={"feedback": feedback})
@app.post("/get_strong_weak_topics")
async def get_strong_weak_topics(email: str):
# """
# API endpoint to identify strong and weak topics for a student.
# Requires at least 10 test attempts. Uses DeepSeek API for analysis.
# Expects 'email' as form data.
# """
df = generate_df()
df_email = df[df['email'] == email]
# Check if the student has attempted at least 10 tests
if len(df_email)<10:
# The original condition was `len(df)>=10`, which seems to check the total number of tests in the DB,
# not for the specific user. Changed to `len(df_email) >= 10`.
return JSONResponse(content={"message": "Please attempt atleast 10 tests to enable this feature"})
elif len(df)>=10:
# Get responses from the latest 10 tests (or all if fewer than 10, but the check above ensures at least 10)
response = df_email['responses'].values[:10]
# Assuming response is a list of responses
formatted_data = str(response) # Convert response to a string format suitable for the API call
section_info = {
'filename': 'student_performance',
'schema': {
'weak_topics': ['Topic#1', 'Topic#2', '...'],
'strong_topics': ['Topic#1', 'Topic#2', '...']
}
}
# Generate response using the client
completion = client.chat.completions.create(
model="deepseek-chat",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": f"""You are an Educational Performance Analyst focusing on {section_info['filename'].replace('_', ' ')}.
Analyze the provided student responses to identify and categorize topics into 'weak' and 'strong' based on their performance. Try to give
high level topics like algebra, trignometry, geometry etc in your response.
Do not add any explanations, introduction, or comments - return ONLY valid JSON.
"""
},
{
"role": "user",
"content": f"""
Here is the raw data for {section_info['filename']}:
{formatted_data}
Convert this data into JSON that matches this schema:
{json.dumps(section_info['schema'], indent=2)}
"""
}
],
temperature=0.0 # Set temperature to 0 for deterministic output
)
# Extract the JSON content from the completion object
strong_weak_topics = completion.choices[0].message.content # Access the content attribute directly
return JSONResponse(content=json.loads(strong_weak_topics))
else:
return JSONResponse(content={"error": "No test results found for this email"})
@app.post("/generate_flashcards")
async def generate_flashcards(email: str):
"""
API endpoint to generate flashcards for a student's weak topics.
Requires at least 10 test attempts.
First, it identifies weak topics using DeepSeek.
Then, it generates flashcards for these topics using Gemini.
Expects 'email' as form data.
"""
df = generate_df() # Get all test results
df_email = df[df['email'] == email]
if len(df_email) < 10:
return JSONResponse(content={"message": "Please attempt at least 10 tests to enable flashcard generation."})
# Step 1: Get the weak topics via DeepSeek
# Get responses from the latest 10 tests
response = df_email['responses'].values[:10]
formatted_data = str(response)
schema = {
'weak_topics': ['Topic#1', 'Topic#2', '...'],
'strong_topics': ['Topic#1', 'Topic#2', '...']
}
completion = client.chat.completions.create(
model="deepseek-chat",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": f"""You are an Educational Performance Analyst focusing on student performance.
Analyze the provided student responses to identify and categorize topics into 'weak' and 'strong' based on their performance.
Do not add any explanations - return ONLY valid JSON."""
},
{
"role": "user",
"content": f"""
Here is the raw data:
{formatted_data}
Convert this data into JSON that matches this schema:
{json.dumps(schema, indent=2)}
"""
}
],
temperature=0.0
)
# Extract weak topics
strong_weak_json = json.loads(completion.choices[0].message.content)
weak_topics = strong_weak_json.get("weak_topics", [])
if not weak_topics:
return JSONResponse(content={"message": "Could not extract weak topics."})
# Step 2: Generate flashcards using Gemini for the identified weak topics
topic_str = ", ".join(weak_topics)
flashcard_prompt = f"""Create 5 concise, simple, straightforward and distinct Anki cards to study the following topic, each with a front and back.
Avoid repeating the content in the front on the back of the card. Avoid explicitly referring to the author or the article.
Use the following format:
Front: [front section of card 1]
Back: [back section of card 1]
...
The topics: {topic_str}
"""
flashcard_response = model.generate_content(flashcard_prompt)
# Step 3: Parse Gemini response into JSON format
flashcards_raw = flashcard_response.text.strip()
flashcard_pattern = re.findall(r"Front:\s*(.*?)\nBack:\s*(.*?)(?=\nFront:|\Z)", flashcards_raw, re.DOTALL)
# Use regex to find all "Front:" and "Back:" pairs
flashcards = [{"Front": front.strip(), "Back": back.strip()} for front, back in flashcard_pattern]
return JSONResponse(content=flashcards)
@app.post("/generate_detailed_summary")
async def generate_detailed_summary(email: str):
"""
API endpoint to generate detailed summaries for a student's weak topics.
Requires at least 10 test attempts.
First, it identifies weak topics using DeepSeek.
Then, it generates summaries for these topics using Gemini.
Expects 'email' as form data.
"""
df = generate_df()
df_email = df[df['email'] == email]
if len(df_email) < 10:
return JSONResponse(content={"message": "Please attempt at least 10 tests to enable detailed summary generation."})
# Step 1: Get the weak topics via DeepSeek
response = df_email['responses'].values[:10]
formatted_data = str(response)
schema = {
'weak_topics': ['Topic#1', 'Topic#2', '...'],
'strong_topics': ['Topic#1', 'Topic#2', '...']
}
completion = client.chat.completions.create(
model="deepseek-chat",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": f"""You are an Educational Performance Analyst focusing on student performance.
Analyze the provided student responses to identify and categorize topics into 'weak' and 'strong' based on their performance.
Do not add any explanations - return ONLY valid JSON."""
},
{
"role": "user",
"content": f"""
Here is the raw data:
{formatted_data}
Convert this data into JSON that matches this schema:
{json.dumps(schema, indent=2)}
"""
}
],
temperature=0.0
)
# Extract weak topics
strong_weak_json = json.loads(completion.choices[0].message.content)
weak_topics = strong_weak_json.get("weak_topics", [])
if not weak_topics:
return JSONResponse(content={"message": "Could not extract weak topics."})
# Step 2: Generate flashcards using Gemini
topic_str = ", ".join(weak_topics)
# flashcard_prompt = f"""Create 5 concise, simple, straightforward and distinct Anki cards to study the following topic, each with a front and back.
# Avoid repeating the content in the front on the back of the card. Avoid explicitly referring to the author or the article.
# Use the following format:
# Front: [front section of card 1]
# Back: [back section of card 1]
# ...
# The topics: {topic_str}
# """
# flashcard_response = model.generate_content(flashcard_prompt)
# # Step 3: Parse Gemini response into JSON format
# flashcards_raw = flashcard_response.text.strip()
# flashcard_pattern = re.findall(r"Front:\s*(.*?)\nBack:\s*(.*?)(?=\nFront:|\Z)", flashcards_raw, re.DOTALL)
# flashcards = [{"Front": front.strip(), "Back": back.strip()} for front, back in flashcard_pattern]
summarization_prompt = f"""
Write an informative and concise summary (approximately 200 words) for each of the following topics.
Do not mention the author or source. Use clear and academic language.
List of topics:
{topic_str}
Use the following format:
Topic: [topic name]
Summary: [200-word summary]
"""
summary_response = model.generate_content(summarization_prompt)
# Step 3: Parse response into JSON format
summary_raw = summary_response.text.strip()
summary_pattern = re.findall(r"Topic:\s*(.*?)\nSummary:\s*(.*?)(?=\nTopic:|\Z)", summary_raw, re.DOTALL)
summaries = [{topic.strip(): summary.strip() for topic, summary in summary_pattern}]
return JSONResponse(content=summaries)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |