Ilayda-j commited on
Commit
361c9e1
1 Parent(s): 96b2682

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -23
app.py CHANGED
@@ -8,7 +8,7 @@ openai.api_key = "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"
8
  def generate_question_and_answer(text):
9
  response = openai.Completion.create(
10
  engine="gpt-3.5-turbo-0301",
11
- prompt=f"Create a question based on the following text: \"{text}\".\nQuestion: ",
12
  max_tokens=50,
13
  n=1,
14
  )
@@ -16,13 +16,14 @@ def generate_question_and_answer(text):
16
 
17
  response = openai.Completion.create(
18
  engine="gpt-3.5-turbo-0301",
19
- prompt=f"What is the answer to the following question based on the text: \"{text}\"?\nQuestion: {question}\nAnswer: ",
20
  max_tokens=50,
21
  n=1,
22
  )
23
- answer = response.choices[0].text.strip()
24
-
25
- return question, answer
 
26
 
27
  def get_feedback(text=None, user_answer=None, continue_quiz=None, state=None):
28
  if state is None:
@@ -33,30 +34,17 @@ def get_feedback(text=None, user_answer=None, continue_quiz=None, state=None):
33
 
34
  if state['step'] == 'get_text':
35
  if text:
36
- state['question'], state['correct_answer'] = generate_question_and_answer(text)
37
  state['step'] = 'get_answer'
38
- return f"Question: {state['question']}", state
39
-
40
- elif state['step'] == 'get_answer':
41
- if user_answer is not None:
42
- feedback = "Correct!" if user_answer.lower() == state['correct_answer'].lower() else f"Incorrect! The correct answer was: {state['correct_answer']}"
43
- state['step'] = 'continue_quiz'
44
- return f"Feedback: {feedback}\nDo you want to answer another question?", state
45
-
46
- elif state['step'] == 'continue_quiz':
47
- if continue_quiz:
48
- if continue_quiz.lower() == 'no':
49
- state['step'] = 'get_text'
50
- return "Quiz ended. Thank you for participating! Please enter new text to start again.", state
51
- else:
52
- state['step'] = 'get_text'
53
- return "Please input text to generate a new question.", state
54
 
55
  iface = gr.Interface(
56
  fn=get_feedback,
57
  inputs=[
58
  gr.inputs.Textbox(lines=5, label="Input Text"),
59
- gr.inputs.Textbox(lines=1, label="Your Answer"),
60
  gr.inputs.Radio(choices=["Yes", "No"], label="Continue?")
61
  ],
62
  outputs=[
@@ -67,3 +55,4 @@ iface = gr.Interface(
67
  )
68
 
69
  iface.launch()
 
 
8
  def generate_question_and_answer(text):
9
  response = openai.Completion.create(
10
  engine="gpt-3.5-turbo-0301",
11
+ prompt=f"Create a multiple-choice question based on the following text: \"{text}\".\nQuestion: ",
12
  max_tokens=50,
13
  n=1,
14
  )
 
16
 
17
  response = openai.Completion.create(
18
  engine="gpt-3.5-turbo-0301",
19
+ prompt=f"Generate 4 possible answers for the question: \"{question}\" based on the text: \"{text}\".\n1. Answer A: \n2. Answer B: \n3. Answer C: \n4. Answer D: ",
20
  max_tokens=50,
21
  n=1,
22
  )
23
+ answers_text = response.choices[0].text.strip()
24
+ answers = answers_text.split("\n")
25
+
26
+ return question, answers
27
 
28
  def get_feedback(text=None, user_answer=None, continue_quiz=None, state=None):
29
  if state is None:
 
34
 
35
  if state['step'] == 'get_text':
36
  if text:
37
+ state['question'], state['answers'] = generate_question_and_answer(text)
38
  state['step'] = 'get_answer'
39
+ return f"Question: {state['question']}\nOptions:\n" + "\n".join(state['answers']), state
40
+ else:
41
+ return "Please input text to generate a question.", state
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  iface = gr.Interface(
44
  fn=get_feedback,
45
  inputs=[
46
  gr.inputs.Textbox(lines=5, label="Input Text"),
47
+ gr.inputs.Radio(choices=["1", "2", "3", "4"], label="Your Answer"),
48
  gr.inputs.Radio(choices=["Yes", "No"], label="Continue?")
49
  ],
50
  outputs=[
 
55
  )
56
 
57
  iface.launch()
58
+