Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
os.environ['GOOGLE_API_KEY'] = "AIzaSyA5LzhPqWiUEEms-AnlYqI75C8YhggrMkw"
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import openai
|
| 7 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 8 |
+
openai.api_key = "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"
|
| 9 |
+
|
| 10 |
+
def get_completion_ai(prompt, model="gpt-3.5-turbo"):
|
| 11 |
+
messages = [{"role": "user", "content": prompt}]
|
| 12 |
+
response = openai.ChatCompletion.create(
|
| 13 |
+
model=model,
|
| 14 |
+
messages=messages,
|
| 15 |
+
temperature=0, # this is the degree of randomness of the model's output
|
| 16 |
+
)
|
| 17 |
+
return response.choices[0].message["content"]
|
| 18 |
+
|
| 19 |
+
def evaluate_answer(question,answer,context):
|
| 20 |
+
prompt_sum = f'''Given the {context}, check whether the answer {answer} is the correct answer to the question {question}.
|
| 21 |
+
Here are the steps:
|
| 22 |
+
1.Extract the {question} and its answer from the provided context
|
| 23 |
+
2.Now compare the answer in the context with the {answer} provided by the user
|
| 24 |
+
3.If the answer is correct,
|
| 25 |
+
tell the user that he did a good job. If the answer is wrong, use the same tone to come up with the explanation.'''
|
| 26 |
+
response = get_completion_ai(prompt_sum)
|
| 27 |
+
return response
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
gemini_model = ChatGoogleGenerativeAI(model = "gemini-pro")
|
| 32 |
+
def generate_multiple_questions(topic):
|
| 33 |
+
prompt_sum = f'''Given the {topic}, generate atleast 10 multiple choice questions that test the users understanding of the topic. Here is the format:
|
| 34 |
+
|
| 35 |
+
Question:
|
| 36 |
+
Four Multiple choice options:
|
| 37 |
+
Correct answer:
|
| 38 |
+
|
| 39 |
+
Here is an example:
|
| 40 |
+
Question: Which of the following statements is NOT a consequence of Newton's third law?
|
| 41 |
+
Options:
|
| 42 |
+
A. For every action, there is an equal and opposite reaction.
|
| 43 |
+
B. The total momentum of a closed system remains constant.
|
| 44 |
+
C. The force of gravity between two objects is directly proportional to their masses.
|
| 45 |
+
D. The force of friction is always in the direction opposite to the motion of the object.
|
| 46 |
+
Correct Answer: The force of gravity between two objects is directly proportional to their masses'''
|
| 47 |
+
response = get_completion(prompt_sum)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
def generate_questions(topic):
|
| 51 |
+
# mcqs = qna_engine.generate_mcq(topic = topic,
|
| 52 |
+
# level = "Advanced",
|
| 53 |
+
# num = 10,
|
| 54 |
+
# llm = gemini_model
|
| 55 |
+
# )
|
| 56 |
+
mcqs = generate_multiple_questions(topic)
|
| 57 |
+
print(f"Context: {mcqs}")
|
| 58 |
+
question = extract_questions(mcqs)
|
| 59 |
+
return question, mcqs
|
| 60 |
+
def get_completion(text):
|
| 61 |
+
|
| 62 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 63 |
+
response = model.generate_content(text)
|
| 64 |
+
return response.text
|
| 65 |
+
|
| 66 |
+
def extract_questions(mcq):
|
| 67 |
+
prompt_sum = f'''Given questions {mcq}, select one question and present to the user with the options. Do not show the correct answer.'''
|
| 68 |
+
response = get_completion(prompt_sum)
|
| 69 |
+
return response
|
| 70 |
+
|
| 71 |
+
import gradio as gr
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# Agent Socrates: Evaluate your understanding of a topic 🔥")
|
| 75 |
+
caption = gr.Textbox(label="Enter a topic of your choice ")
|
| 76 |
+
btn_caption = gr.Button("Generate a question")
|
| 77 |
+
questions = gr.Textbox(label="Here is the question. Now answer")
|
| 78 |
+
context = gr.Textbox(label="Here is the context", visible=False)
|
| 79 |
+
btn_caption.click(fn=generate_questions, inputs=caption, outputs=[questions,context])
|
| 80 |
+
print (context)
|
| 81 |
+
#audio_input = [gr.Audio(source="microphone", type="filepath", label="Start speaking"),caption]
|
| 82 |
+
|
| 83 |
+
answer = gr.Textbox(label = "Enter your answer")
|
| 84 |
+
|
| 85 |
+
btn_submit = gr.Button("Evaluate your response")
|
| 86 |
+
evaluation_report = gr.Textbox(label="Your evaluation report would show up here")
|
| 87 |
+
btn_submit.click(fn=evaluate_answer, inputs=[questions, answer, context], outputs=[evaluation_report])
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
gr.close_all()
|
| 91 |
+
#demo.launch(share=True, debug=True)
|
| 92 |
+
# Launch the Gradio app
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.queue(max_size=20).launch(debug=True)
|