sathwikabhavaraju2005 commited on
Commit
db2cb55
·
verified ·
1 Parent(s): cc8dc6d

Update utils/quiz_generator.py

Browse files
Files changed (1) hide show
  1. utils/quiz_generator.py +13 -19
utils/quiz_generator.py CHANGED
@@ -1,23 +1,17 @@
1
  import os
2
- from openai import OpenAI
3
- from dotenv import load_dotenv
4
 
5
- # Load your API key from .env
6
- load_dotenv()
7
- api_key = os.getenv("OPENAI_API_KEY")
8
- client = OpenAI(api_key=api_key)
9
 
10
- def generate_quiz(topic, num_questions=5):
11
- prompt = f"Generate {num_questions} multiple-choice questions from the following content. Include 4 options and indicate the correct answer:\n\n{topic}"
 
 
 
12
 
13
- try:
14
- response = client.chat.completions.create(
15
- model="gpt-3.5-turbo",
16
- messages=[
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"]