Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,8 +36,19 @@ def generate_mcqs_on_topic(text, topic, num_mcqs=5):
|
|
36 |
|
37 |
return mcqs
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def generate_question_with_chatgpt(context, topic):
|
40 |
-
client=OpenAI()
|
41 |
# Initializing the default value
|
42 |
generated_question = {
|
43 |
'content': "Unable to generate a question..",
|
@@ -48,26 +59,17 @@ def generate_question_with_chatgpt(context, topic):
|
|
48 |
response = client.chat.completions.create(
|
49 |
model="gpt-3.5-turbo",
|
50 |
max_tokens=1024,
|
51 |
-
temperature
|
52 |
-
messages
|
53 |
{"role": "system", "content": "You are a helpful assistant."},
|
54 |
{"role": "user", "content": f"What is the question on {topic} for the following? {context}"},
|
55 |
]
|
56 |
-
|
57 |
)
|
58 |
-
result=response.json()
|
59 |
|
60 |
print("API Response:", result) # Add this line for debugging
|
61 |
|
62 |
-
|
63 |
-
# Extract the generated question, options, and correct answer from the response
|
64 |
-
generated_question = {
|
65 |
-
'content': result["choices"][0]["message"]["content"],
|
66 |
-
'options': result["choices"][0]["message"].get("options", []),
|
67 |
-
'correct_answer': result["choices"][0]["message"].get("correct_answer", "Unknown")
|
68 |
-
}
|
69 |
-
else:
|
70 |
-
print("Unexpected API response format.")
|
71 |
|
72 |
return generated_question
|
73 |
|
@@ -102,6 +104,7 @@ if __name__ == "__main__":
|
|
102 |
|
103 |
|
104 |
|
|
|
105 |
|
106 |
|
107 |
|
|
|
36 |
|
37 |
return mcqs
|
38 |
|
39 |
+
def extract_options_and_correct_answer(api_response):
|
40 |
+
if 'choices' in api_response:
|
41 |
+
message = api_response['choices'][0]['message']
|
42 |
+
content = message.get('content', "Unable to generate a question..")
|
43 |
+
options = message.get('options', [])
|
44 |
+
correct_answer = message.get('correct_answer', "Unknown")
|
45 |
+
|
46 |
+
return content, options, correct_answer
|
47 |
+
|
48 |
+
return "Unexpected API response format.", [], "Unknown"
|
49 |
+
|
50 |
def generate_question_with_chatgpt(context, topic):
|
51 |
+
client = OpenAI()
|
52 |
# Initializing the default value
|
53 |
generated_question = {
|
54 |
'content': "Unable to generate a question..",
|
|
|
59 |
response = client.chat.completions.create(
|
60 |
model="gpt-3.5-turbo",
|
61 |
max_tokens=1024,
|
62 |
+
temperature=0.7,
|
63 |
+
messages=[
|
64 |
{"role": "system", "content": "You are a helpful assistant."},
|
65 |
{"role": "user", "content": f"What is the question on {topic} for the following? {context}"},
|
66 |
]
|
|
|
67 |
)
|
68 |
+
result = response.json()
|
69 |
|
70 |
print("API Response:", result) # Add this line for debugging
|
71 |
|
72 |
+
generated_question['content'], generated_question['options'], generated_question['correct_answer'] = extract_options_and_correct_answer(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
return generated_question
|
75 |
|
|
|
104 |
|
105 |
|
106 |
|
107 |
+
|
108 |
|
109 |
|
110 |
|