Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_app.py
|
2 |
+
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from typing import List, Optional
|
6 |
+
from langchain.document_loaders import WikipediaLoader
|
7 |
+
from langchain_groq import ChatGroq
|
8 |
+
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Replace with your actual Groq API key
|
12 |
+
GROQ_API_KEY = "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"
|
13 |
+
|
14 |
+
class QuizRequest(BaseModel):
|
15 |
+
search_query: str
|
16 |
+
complexity: str = "Hard"
|
17 |
+
quiz_type: str = "MCQs"
|
18 |
+
|
19 |
+
class GradeRequest(BaseModel):
|
20 |
+
quiz: str
|
21 |
+
answers: str
|
22 |
+
context: Optional[str] = None
|
23 |
+
|
24 |
+
def get_llm():
|
25 |
+
return ChatGroq(
|
26 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
27 |
+
temperature=0,
|
28 |
+
max_tokens=1024,
|
29 |
+
api_key=GROQ_API_KEY
|
30 |
+
)
|
31 |
+
|
32 |
+
def wikipedia_query(search_query):
|
33 |
+
try:
|
34 |
+
data = WikipediaLoader(query=search_query, load_max_docs=2).load()
|
35 |
+
return data
|
36 |
+
except Exception as e:
|
37 |
+
raise HTTPException(status_code=500, detail=f"Wikipedia query failed: {e}")
|
38 |
+
|
39 |
+
@app.post("/generate_quiz/")
|
40 |
+
async def generate_quiz(request: QuizRequest):
|
41 |
+
context = wikipedia_query(request.search_query)
|
42 |
+
llm = get_llm()
|
43 |
+
|
44 |
+
prompt = f"""
|
45 |
+
You are a quiz generator assistant. Create a quiz for the given context.
|
46 |
+
|
47 |
+
Instructions:
|
48 |
+
Do not write answers in the quiz.
|
49 |
+
Quiz should be based on the following context:
|
50 |
+
|
51 |
+
context:
|
52 |
+
{context}
|
53 |
+
|
54 |
+
quiz complexity:
|
55 |
+
{request.complexity}
|
56 |
+
|
57 |
+
quiz type:
|
58 |
+
{request.quiz_type}
|
59 |
+
|
60 |
+
Your response:
|
61 |
+
"""
|
62 |
+
|
63 |
+
result = llm.invoke(prompt)
|
64 |
+
return {"quiz": result.content, "context": str(context)}
|
65 |
+
|
66 |
+
@app.post("/grade_quiz/")
|
67 |
+
async def grade_quiz(request: GradeRequest):
|
68 |
+
llm = get_llm()
|
69 |
+
|
70 |
+
prompt = f"""
|
71 |
+
Check the quiz answers and give marks and also provide the correct answers. Use the following context to check the quiz. Write only the marks.
|
72 |
+
|
73 |
+
quiz:
|
74 |
+
{request.quiz}
|
75 |
+
|
76 |
+
answers:
|
77 |
+
{request.answers}
|
78 |
+
|
79 |
+
context:
|
80 |
+
{request.context}
|
81 |
+
"""
|
82 |
+
|
83 |
+
result = llm.invoke(prompt)
|
84 |
+
return {"grade": result.content}
|