Ilayda-j commited on
Commit
a099ff3
1 Parent(s): 7e588a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -16
app.py CHANGED
@@ -5,36 +5,60 @@ import openai
5
  # Replace with your OpenAI API key
6
  openai.api_key = "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"
7
 
 
 
 
 
 
 
 
 
 
 
 
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
  )
15
- question = response.choices[0].text.strip()
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, user_answer=None, continue_quiz='Yes'):
28
- if continue_quiz.lower() == 'no':
29
- return "Quiz ended. Thank you for participating!"
30
 
31
- question, correct_answer = generate_question_and_answer(text)
32
-
33
- if user_answer is not None:
34
- feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}"
35
- return f"Question: {question}\nFeedback: {feedback}"
36
- else:
37
- return f"Question: {question}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  iface = gr.Interface(
40
  fn=get_feedback,
@@ -43,7 +67,12 @@ iface = gr.Interface(
43
  gr.inputs.Textbox(lines=1, label="Your Answer"),
44
  gr.inputs.Radio(choices=["Yes", "No"], label="Continue?")
45
  ],
46
- outputs=gr.outputs.Textbox(label="Model Feedback"),
 
 
 
 
 
47
  )
48
 
49
- iface.launch()
 
5
  # Replace with your OpenAI API key
6
  openai.api_key = "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"
7
 
8
+ # Global variables to store the state
9
+ current_step = "question_generation"
10
+ current_question = ""
11
+ correct_answer = ""
12
+
13
+ def reset_state():
14
+ global current_step, current_question, correct_answer
15
+ current_step = "question_generation"
16
+ current_question = ""
17
+ correct_answer = ""
18
+
19
  def generate_question_and_answer(text):
20
+ global current_question, correct_answer
21
+
22
  response = openai.Completion.create(
23
  engine="gpt-3.5-turbo-0301",
24
  prompt=f"Create a question based on the following text: \"{text}\".\nQuestion: ",
25
  max_tokens=50,
26
  n=1,
27
  )
28
+ current_question = response.choices[0].text.strip()
29
 
30
  response = openai.Completion.create(
31
  engine="gpt-3.5-turbo-0301",
32
+ prompt=f"What is the answer to the following question based on the text: \"{text}\"?\nQuestion: {current_question}\nAnswer: ",
33
  max_tokens=50,
34
  n=1,
35
  )
36
+ correct_answer = response.choices[0].text.strip()
 
 
37
 
38
  def get_feedback(text, user_answer=None, continue_quiz='Yes'):
39
+ global current_step, current_question, correct_answer
 
40
 
41
+ if current_step == "question_generation":
42
+ if text:
43
+ generate_question_and_answer(text)
44
+ current_step = "answer_checking"
45
+ return f"Question: {current_question}", "", "hidden"
46
+ else:
47
+ return "Please input text to generate a question.", "", "hidden"
48
+ elif current_step == "answer_checking":
49
+ if user_answer is not None:
50
+ feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}"
51
+ current_step = "continue_prompt"
52
+ return f"Feedback: {feedback}", "Do you want to answer another question?", "text"
53
+ else:
54
+ return f"Question: {current_question}", "", "hidden"
55
+ elif current_step == "continue_prompt":
56
+ if continue_quiz.lower() == 'no':
57
+ reset_state()
58
+ return "Quiz ended. Thank you for participating!", "", "hidden"
59
+ else:
60
+ current_step = "question_generation"
61
+ return "Please input text to generate a new question.", "", "hidden"
62
 
63
  iface = gr.Interface(
64
  fn=get_feedback,
 
67
  gr.inputs.Textbox(lines=1, label="Your Answer"),
68
  gr.inputs.Radio(choices=["Yes", "No"], label="Continue?")
69
  ],
70
+ outputs=[
71
+ gr.outputs.Textbox(label="Model Output"),
72
+ gr.outputs.Textbox(label="Prompt"),
73
+ gr.outputs.Textbox(label="Continue?", type="hidden")
74
+ ],
75
+ live=True
76
  )
77
 
78
+ iface.launch()