PhysicsExp2 / app.py
BhardwajMayank's picture
Create app.py
df59be7 verified
Raw History Blame Contribute Delete
4.62 kB
import os
import anthropic
from dotenv import load_dotenv
from IPython.display import Markdown , display
import gradio as gr
import subprocess
anthropic_key = os.environ.get("ANTHROPIC_API_KEY")
claude = anthropic.Anthropic(api_key=anthropic_key)
claude_model = "claude-3-5-sonnet-20241022"
system_message = "you are a great high school physics teacher , you explain topics in \
in very simple way ensuring the person's fundamental regarding the topic is also clear."
system_message += "\n you also provide relevant simplest example with your explanations."
system_message += "\n If asked a physics numerical , you provide simple step by \
step explanation and make sure answer is correct."
system_message += "\n Also try to give explanation in no more than 1000 words"
def explanation(topic):
result = claude.messages.stream(
model=claude_model,
system=system_message,
messages=[{"role": "user", "content": f"Sir {topic}"}],
)
output_text = ""
with result as stream:
for text in stream.text_stream:
output_text += text
return output_text
from openai import OpenAI
openai_key = os.environ.get('OPENAI_API_KEY')
openai = OpenAI(api_key = openai_key)
system_message2 = "You are a well known teacher , when someone gives you a topic you give them 5 MCQ questions one by one to test their knowledge on that topic, the next question appears after the user have answered the previous \
one , and at the end you give score out of 5 and teach them in short and in easy terms what they did not know about the topic (based on their answers)"
system_message2 += "\n your main focus is on Indian ICSE board High class 9 physics."
system_message2 += "\n in start you just ask humorously like , oh so you think you know all about a topic , tell me the topic , lets see your knowledge , just like that short and crisp , then questions accordingly , and appriciate or motivate at end according to their performance"
system_message2 += "\n Make sure to explain the concept of the question where they did wrong at end."
def gpt_qna(message, history):
messages = [{"role" : "system" , "content" : system_message2}]
for user_message , assistant_message in history:
messages.append({"role" : "user" , "content" : user_message})
messages.append({"role" : "assistant" , "content" : assistant_message})
messages.append({"role" : "user" , "content" : message})
stream = openai.chat.completions.create(model = 'gpt-4o-mini' , messages = messages, stream = True)
response = ""
for chunk in stream:
response += chunk.choices[0].delta.content or ''
yield response
from paddleocr import PaddleOCR
def ocr_with_paddle(img):
finaltext = ''
ocr = PaddleOCR(lang='en', use_angle_cls=False)
result = ocr.ocr(img)
for i in range(len(result[0])):
text = result[0][i][1][0]
finaltext += ' ' + text
return finaltext
def explanation2(topic):
result = claude.messages.create(
model = claude_model,
system = system_message,
messages = [
{'role' : 'user' , 'content' : f'can you answer the following question in easy terms : {topic}'}
]
)
return result.content[0].text
def process_image_and_answer(img):
extracted_text = ocr_with_paddle(img)
gpt_answer = explanation2(extracted_text)
return gpt_answer
# Gradio Interface
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
topic_input = gr.Textbox(label="Enter your Topic:", placeholder="Type your topic/Question here...")
topic_output = gr.Markdown(label="Topic Explanation")
explain_button = gr.Button("Explain Topic")
explain_button.click(fn=explanation, inputs=topic_input, outputs=topic_output)
with gr.Column():
custom_chatbot = gr.Chatbot(height=600)
chat_interface = gr.ChatInterface(fn=gpt_qna, chatbot=custom_chatbot)
with gr.Row():
gr.Button("Clear").click(lambda: None, None, [topic_output])
with gr.Row():
with gr.Column():
gr.Markdown("### Upload a Question Image for AI Explanation")
image_input = gr.Image(label="Upload Question Image")
gpt_answer_output = gr.Markdown(label="Answer from ChatGPT")
process_button = gr.Button("Process Image")
process_button.click(fn=process_image_and_answer, inputs=image_input, outputs=gpt_answer_output)
with gr.Row():
gr.Button("Clear").click(lambda: None, None, [topic_output, gpt_answer_output])
demo.launch(debug=True)