Rename utils/quiz_generator.py to utils/quiz_offline.py
Browse files- utils/quiz_generator.py +0 -17
- utils/quiz_offline.py +28 -0
utils/quiz_generator.py
DELETED
@@ -1,17 +0,0 @@
|
|
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"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/quiz_offline.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
2 |
+
|
3 |
+
# Load the model and tokenizer
|
4 |
+
model_name = "t5-base" # lightweight and works offline
|
5 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
6 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
7 |
+
|
8 |
+
def generate_mcqs(text, num_questions=3):
|
9 |
+
input_text = f"generate questions: {text}"
|
10 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
11 |
+
|
12 |
+
outputs = model.generate(
|
13 |
+
input_ids=input_ids,
|
14 |
+
max_length=256,
|
15 |
+
num_return_sequences=1,
|
16 |
+
temperature=0.7
|
17 |
+
)
|
18 |
+
|
19 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
+
return decoded.strip()
|
21 |
+
|
22 |
+
# π½ TEST EXAMPLE π½
|
23 |
+
if __name__ == "__main__":
|
24 |
+
sample = """
|
25 |
+
The process of photosynthesis allows plants to convert sunlight, water, and carbon dioxide into food. This process takes place in the chloroplasts and releases oxygen as a byproduct.
|
26 |
+
"""
|
27 |
+
print("π Quiz Output:\n")
|
28 |
+
print(generate_mcqs(sample))
|