|
import os |
|
import gradio as gr |
|
from transformers import pipeline |
|
from PIL import Image |
|
import pytesseract |
|
|
|
|
|
if not os.path.exists("/usr/bin/tesseract"): |
|
os.system("apt-get update && apt-get install -y tesseract-ocr") |
|
|
|
|
|
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" |
|
|
|
|
|
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) |
|
|
|
|
|
demo.launch() |
|
|
|
|