Spaces:
Running
Running
import os | |
import openai | |
os.environ["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) | |