Hammad712 commited on
Commit
5edff09
·
verified ·
1 Parent(s): 7e15f65

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -36
main.py CHANGED
@@ -2,24 +2,28 @@
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(
@@ -29,56 +33,75 @@ def get_llm():
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}
 
 
 
2
 
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
+ from typing import Optional
6
  from langchain.document_loaders import WikipediaLoader
7
  from langchain_groq import ChatGroq
8
+ import os
9
 
10
+ app = FastAPI(title="Quiz Generator API")
11
 
12
+ # in‑memory store for the last quiz + context
13
+ STORE = {
14
+ "quiz": None, # str
15
+ "context": None, # str
16
+ }
17
 
18
+ # Replace with your actual Groq API key
19
+ GROQ_API_KEY = os.genenv('api_key')
20
  class QuizRequest(BaseModel):
21
  search_query: str
22
  complexity: str = "Hard"
23
  quiz_type: str = "MCQs"
24
 
25
  class GradeRequest(BaseModel):
 
26
  answers: str
 
27
 
28
  def get_llm():
29
  return ChatGroq(
 
33
  api_key=GROQ_API_KEY
34
  )
35
 
36
+ def wikipedia_query(search_query: str):
37
  try:
38
+ docs = WikipediaLoader(query=search_query, load_max_docs=2).load()
39
+ return docs
40
  except Exception as e:
41
  raise HTTPException(status_code=500, detail=f"Wikipedia query failed: {e}")
42
 
43
+ @app.get("/")
44
+ async def root():
45
+ return {
46
+ "message": "Welcome to the Quiz Generator API! \n"
47
+ "POST /generate_quiz/ to create a new quiz. \n"
48
+ "POST /grade_quiz/ to grade your answers against the last quiz."
49
+ }
50
+
51
  @app.post("/generate_quiz/")
52
  async def generate_quiz(request: QuizRequest):
53
+ # fetch & store context
54
+ context_docs = wikipedia_query(request.search_query)
55
+ context_text = str(context_docs)
56
+ STORE["context"] = context_text
57
 
58
+ # generate quiz
59
+ llm = get_llm()
60
  prompt = f"""
61
+ You are a quiz generator assistant. Create a quiz for the given context.
62
 
63
+ Instructions:
64
+ - Do not write answers in the quiz.
65
+ - Quiz should be based on the following context:
66
 
67
+ context:
68
+ {context_text}
69
 
70
+ quiz complexity:
71
+ {request.complexity}
72
 
73
+ quiz type:
74
+ {request.quiz_type}
 
 
 
75
 
76
+ Your response:
77
+ """
78
  result = llm.invoke(prompt)
79
+ STORE["quiz"] = result.content
80
+
81
+ return {
82
+ "quiz": result.content
83
+ }
84
 
85
  @app.post("/grade_quiz/")
86
  async def grade_quiz(request: GradeRequest):
87
+ # ensure we have a quiz to grade
88
+ if STORE["quiz"] is None or STORE["context"] is None:
89
+ raise HTTPException(status_code=400, detail="No quiz available. Call /generate_quiz/ first.")
90
 
91
+ llm = get_llm()
92
  prompt = f"""
93
+ Check the quiz answers and give marks and also provide the correct answers. Use the following context to check the quiz. Return only the total mark and the correct answers.
 
 
 
94
 
95
+ quiz:
96
+ {STORE['quiz']}
97
 
98
+ answers:
99
+ {request.answers}
 
100
 
101
+ context:
102
+ {STORE['context']}
103
+ """
104
  result = llm.invoke(prompt)
105
+ return {
106
+ "grade": result.content
107
+ }