Leo9 / app.py
ehi20011's picture
Update app.py
0f5e2f6 verified
import os
import gradio as gr
from transformers import pipeline
from PIL import Image
import pytesseract
# Install tesseract-ocr if not available (for HF Spaces or Colab)
if not os.path.exists("/usr/bin/tesseract"):
os.system("apt-get update && apt-get install -y tesseract-ocr")
# Load chatbot
generator = pipeline("text-generation", model="EleutherAI/gpt-neo-125M")
def chat_response(message, history=[]):
prompt = f"Human: {message}\nAI:"
result = generator(prompt, max_length=100, do_sample=True, temperature=0.7)[0]['generated_text']
reply = result.split("AI:")[-1].strip()
history.append((message, reply))
return history, history
def solve_assignment(image):
text = pytesseract.image_to_string(image)
response = generator(f"Solve this problem: {text}", max_length=100, do_sample=True)[0]['generated_text']
return f"๐Ÿง  Extracted Text:\n{text}\n\n๐Ÿ“˜ Solution:\n{response}"
def download_notes():
content = "Here is your custom downloadable content.\nStudy hard and succeed!"
with open("notes.txt", "w") as f:
f.write(content)
return "notes.txt"
def quiz_game():
question = "What is 5 + 7?"
return question, "12"
# UI Layout
with gr.Blocks(title="Leo9 AI Tutor") as demo:
gr.Markdown("## ๐Ÿ‘‹ Welcome to Leo9 AI โ€” Your Smart Tutor")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Ask me anything in any language")
clear = gr.Button("Clear")
msg.submit(chat_response, [msg, chatbot], [chatbot, chatbot])
clear.click(lambda: None, None, chatbot)
gr.Markdown("---")
gr.Markdown("### ๐Ÿ“ธ Upload Homework for Solving")
image_input = gr.Image(type="pil", label="Upload assignment image")
image_output = gr.Textbox(label="AI Solution")
image_input.change(solve_assignment, inputs=image_input, outputs=image_output)
gr.Markdown("---")
gr.Markdown("### ๐ŸŽฎ Gamified Quiz (Example)")
quiz_btn = gr.Button("Get a Quiz Question")
quiz_q = gr.Textbox(label="Quiz Question")
quiz_a = gr.Textbox(label="Answer")
quiz_btn.click(quiz_game, outputs=[quiz_q, quiz_a])
gr.Markdown("---")
gr.Markdown("### ๐Ÿ“ฅ Download Learning Notes")
dl_btn = gr.Button("Download Notes")
file_output = gr.File()
dl_btn.click(download_notes, outputs=file_output)
# Launch app
demo.launch()