Update utils/quiz_generator.py
Browse files- utils/quiz_generator.py +13 -19
utils/quiz_generator.py
CHANGED
@@ -1,23 +1,17 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
from dotenv import load_dotenv
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
client = OpenAI(api_key=api_key)
|
9 |
|
10 |
-
def generate_quiz(topic, num_questions
|
11 |
-
prompt = f"Generate {num_questions}
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
response
|
15 |
-
|
16 |
-
|
17 |
-
{"role": "system", "content": "You are a quiz-generating assistant."},
|
18 |
-
{"role": "user", "content": prompt}
|
19 |
-
]
|
20 |
-
)
|
21 |
-
return response.choices[0].message.content.strip()
|
22 |
-
except Exception as e:
|
23 |
-
return f"Error generating quiz:\n{str(e)}"
|
|
|
1 |
import os
|
2 |
+
import requests
|
|
|
3 |
|
4 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
5 |
+
API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-base"
|
6 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
7 |
|
8 |
+
def generate_quiz(topic, num_questions):
|
9 |
+
prompt = f"Generate {num_questions} MCQ questions based on the following content:\n{topic}"
|
10 |
+
|
11 |
+
payload = {"inputs": prompt}
|
12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
13 |
|
14 |
+
if response.status_code != 200:
|
15 |
+
return f"❌ Error: {response.status_code} - {response.json()}"
|
16 |
+
|
17 |
+
return response.json()[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|