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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -5,49 +5,45 @@ import openai
5
  # Replace with your OpenAI API key
6
  openai.api_key = "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"
7
 
8
- def generate_question(text_content):
9
  response = openai.Completion.create(
10
  engine="gpt-3.5-turbo-0301",
11
- prompt=f"Based on the following text, generate a question:\n{text_content}\nQuestion:",
12
  max_tokens=50,
 
13
  )
14
  question = response.choices[0].text.strip()
15
- return question
16
 
17
- def check_answer(question, answer, text_content):
18
  response = openai.Completion.create(
19
  engine="gpt-3.5-turbo-0301",
20
- prompt=f"The text is: \n{text_content}\nThe question is: {question}\nThe answer given is: {answer}\nIs the answer correct?",
21
- max_tokens=5,
 
22
  )
23
- feedback = response.choices[0].text.strip()
24
- return feedback.lower() == "yes"
 
25
 
26
- def interactive_quiz(file, answer=None, continue_quiz='yes'):
27
- if file is None:
28
- return "Please upload a txt file to start.", "", "hidden"
29
 
30
- text_content = file.read().decode("utf-8")
31
- question = generate_question(text_content)
32
-
33
- if continue_quiz.lower() == 'yes':
34
- if answer is not None:
35
- is_correct = check_answer(question, answer, text_content)
36
- feedback = "Correct!" if is_correct else "Incorrect."
37
- return question, feedback, "text"
38
- else:
39
- return question, "", "text"
40
- else:
41
- return "Thank you for participating!", "", "hidden"
42
 
 
 
 
 
 
43
 
44
  iface = gr.Interface(
45
- fn=interactive_quiz,
46
- inputs=["file", "text", "text"],
47
- outputs=["text", "text", "text"],
48
- live=True
 
 
 
49
  )
50
 
51
-
52
- iface.launch()
53
-
 
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,
41
+ inputs=[
42
+ gr.inputs.Textbox(lines=5, label="Input Text"),
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()