File size: 3,518 Bytes
49bdb87
 
2a23e72
10c3cc3
85e1d21
49bdb87
2a23e72
49bdb87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1c0a63
49bdb87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import openai
os.environ["OPENAI_API_KEY"] = os.getenv('openai')
openai.api_key = os.getenv('openai')


def get_completion(prompt, model="gpt-3.5-turbo"):
  messages = [{"role": "user", "content": prompt}]
  response = openai.ChatCompletion.create(
      model=model,
      messages=messages,
      temperature=0, # this is the degree of randomness of the model's output
  )
  return response.choices[0].message["content"] 

def evaluate_answer(question,answer,context):
  prompt_sum = f'''Given the {context}, check whether the answer {answer} is the correct answer to the question {question}. 
  Here are the steps:
  1.Extract the {question} and its answer from the provided context
  2.Now compare the answer in the context with the {answer} provided by the user
  3.If the answer is correct, 
  tell the user that he did a good job. If the answer is wrong, use the same tone to come up with the explanation.'''
  response = get_completion(prompt_sum)
  return response

    
def generate_multiple_questions(topic):
  prompt_sum = f'''Given the {topic}, generate atleast 10 multiple choice questions that test the users understanding of the topic. Here is the format:
  
  Question: 
  Four Multiple choice options:
  Correct answer:
  
  Here is an example: 
Question: Which of the following statements is NOT a consequence of Newton's third law?
Options:
  A. For every action, there is an equal and opposite reaction.
  B. The total momentum of a closed system remains constant.
  C. The force of gravity between two objects is directly proportional to their masses.
  D. The force of friction is always in the direction opposite to the motion of the object.
Correct Answer: The force of gravity between two objects is directly proportional to their masses'''
  response = get_completion(prompt_sum)
  return response

def generate_questions(topic):
   # mcqs = qna_engine.generate_mcq(topic = topic,
    #                           level = "Advanced",
     #                          num = 10,
      #                         llm = gemini_model
      #                         )
    mcqs = generate_multiple_questions(topic)
    print(f"Context: {mcqs}")
    question = extract_questions(mcqs)
    return question, mcqs

def extract_questions(mcq):
  prompt_sum = f'''Given questions {mcq}, select one question and present to the user with the options. Do not show the correct answer.'''
  response = get_completion(prompt_sum)
  return response

import gradio as gr 

with gr.Blocks() as demo:
    gr.Markdown("# Agent Socrates: Evaluate your understanding of a topic 🔥")
    caption = gr.Textbox(label="Enter a topic of your choice ")
    btn_caption = gr.Button("Generate a question")
    questions = gr.Textbox(label="Here is the question. Now answer")
    context = gr.Textbox(label="Here is the context", visible=False)
    btn_caption.click(fn=generate_questions, inputs=caption, outputs=[questions,context])
    print (context)
    #audio_input = [gr.Audio(source="microphone", type="filepath", label="Start speaking"),caption]
    
    answer = gr.Textbox(label = "Enter your answer")

    btn_submit = gr.Button("Evaluate your response")
    evaluation_report = gr.Textbox(label="Your evaluation report would show up here")
    btn_submit.click(fn=evaluate_answer, inputs=[questions, answer, context], outputs=[evaluation_report])

    
gr.close_all()
#demo.launch(share=True, debug=True)
# Launch the Gradio app
if __name__ == "__main__":
    demo.queue(max_size=20).launch(debug=True)