Spaces:
Sleeping
Sleeping
File size: 7,714 Bytes
f2f27c1 9fdb421 a4860a3 9fdb421 f2f27c1 9fdb421 f2f27c1 4d94ee5 9fdb421 f2f27c1 4d94ee5 f2f27c1 9fdb421 f2f27c1 9fdb421 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 e29c59e 4d94ee5 e29c59e 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 e29c59e 4d94ee5 e29c59e 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 9fdb421 4d94ee5 65cec6c 4d94ee5 9fdb421 65cec6c 9fdb421 4d94ee5 9fdb421 4d94ee5 65cec6c 4d94ee5 65cec6c 4d94ee5 9fdb421 4d94ee5 65cec6c 4d94ee5 65cec6c 9fdb421 4d94ee5 9fdb421 4d94ee5 2dfe7fa 65cec6c 4d94ee5 9fdb421 65cec6c 9fdb421 2dfe7fa 65cec6c 9fdb421 4d94ee5 9fdb421 2dfe7fa 65cec6c 4d94ee5 9fdb421 4d94ee5 2dfe7fa 65cec6c 4d94ee5 2dfe7fa 65cec6c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import os
import openai
import gradio as gr
import pytesseract
from PIL import Image
import random
openai.api_key = os.getenv("OPENAI_API_KEY")
# -- OCR + Auto-fill --
def extract_and_fill(img):
try:
text = pytesseract.image_to_string(img)
return text, text
except Exception as e:
return f"โ Error: {e}", ""
# -- Solver --
def solve_question(q, mode):
if not q:
return "โ ๏ธ Please enter a question.", ""
prompt = {
"๐ก Hint": f"Give a helpful hint. Question: {q}",
"๐ Steps": f"Explain step-by-step. Question: {q}",
"โ
Final": f"Explain and give the final answer. Question: {q}"
}.get(mode, q)
try:
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
full = res.choices[0].message.content.strip()
final = full.split("Final Answer:")[-1].strip() if "Final Answer:" in full else ""
return full, final
except Exception as e:
return f"โ Error: {str(e)}", ""
# -- Game Mode --
def generate_game_question(grade):
prompt = f"Create a {grade} math question with 1 correct and 3 incorrect answers. Format: Question: ... A: ... B: ... C: ... D: ... Correct: ..."
try:
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
text = res.choices[0].message.content.strip()
lines = text.splitlines()
question = [l for l in lines if l.startswith("Question:")][0][9:].strip()
choices = {l.split(":")[0]: l.split(":")[1].strip() for l in lines if l[:1] in "ABCD"}
correct = [l for l in lines if l.startswith("Correct:")][0].split(":")[1].strip()
correct_full = f"{correct}: {choices[correct]}"
shuffled = [f"{k}: {v}" for k, v in random.sample(list(choices.items()), len(choices))]
return question, gr.update(choices=shuffled), correct_full
except Exception as e:
return "โ Error generating question.", gr.update(choices=[]), ""
def check_answer(selected, correct):
if not selected:
return "โ Choose an answer."
return "โ
Correct!" if selected == correct else "โ Try again."
# -- Smart Tutor --
def smart_tutor(q, attempt, mode, concept, grade):
if not q:
return "โ ๏ธ Please enter a question."
prompts = {
"๐ก Just a Hint": f"Give a hint for this {grade} level question: {q}",
"โ
Check My Work": f"My attempt: {attempt}. Question: {q}. Feedback?",
"๐งญ Walk Me Through It": f"Guide me through this {grade} level question: {q}",
"๐ Try a Similar Problem": f"Create a similar problem to: {q}",
"๐ Explain the Concept": f"Explain the concept of '{concept}' for {grade} level."
}
try:
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompts[mode]}]
)
return res.choices[0].message.content.strip()
except Exception as e:
return f"โ Error: {str(e)}"
# -- AI Lab --
def ai_lab(text, mode):
if not text:
return "โ ๏ธ Enter content"
prompts = {
"๐ Predict Difficulty": f"How hard is this? {text}",
"๐ง Classify Topic": f"What topic is this? {text}",
"๐งช Create Practice Set": f"Make 3 problems for: {text}",
"๐ง Explain Confusion Point": f"Why is this confusing: {text}",
"๐ฏ Make Similar Questions": f"Make 2 similar questions: {text}",
"๐ Estimate Grade Level": f"What grade is this? {text}",
"๐ข Predict Step Count": f"How many steps to solve this? {text}",
"๐งฉ Extract Keywords": f"List math keywords in: {text}",
"๐ซ Common Mistakes": f"What mistakes are made with: {text}"
}
try:
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompts[mode]}]
)
return res.choices[0].message.content.strip()
except Exception as e:
return f"โ Error: {str(e)}"
# -- Settings --
settings = {"theme": "default", "grade": "7th Grade", "skills": []}
def save_settings(theme, grade, skills):
settings["theme"] = theme
settings["grade"] = grade
settings["skills"] = skills.split(",")
return f"โ
Settings saved: {settings}"
# -- UI --
with gr.Blocks() as app:
with gr.Tab("๐ญ Solver + Image Upload"):
img = gr.Image(label="๐ธ Upload Image", type="pil", sources=["upload"])
img_text = gr.Textbox(label="๐ OCR Extracted", visible=False)
question = gr.Textbox(label="โ๏ธ Your Question")
mode = gr.Radio(["๐ก Hint", "๐ Steps", "โ
Final"], label="Mode")
response = gr.Textbox(label="๐ค AI Response")
final = gr.Textbox(label="โ
Final Answer")
img.change(extract_and_fill, img, [img_text, question])
gr.Button("๐ญ Think For Me").click(solve_question, [question, mode], [response, final])
gr.Button("๐งน Clear").click(lambda: ("", "", "", ""), None, [question, response, final, img_text])
with gr.Tab("๐ฎ Game"):
grade = gr.Dropdown(["3rd Grade", "4th Grade", "5th Grade", "6th Grade", "7th Grade"], label="Grade", value="7th Grade")
gq = gr.Textbox(label="Question")
gchoices = gr.Radio(label="Choices", choices=[])
gcorrect = gr.Textbox(visible=False)
feedback = gr.Textbox(label="Feedback")
gr.Button("๐ฒ New Question").click(generate_game_question, [grade], [gq, gchoices, gcorrect])
gr.Button("โ
Submit").click(check_answer, [gchoices, gcorrect], feedback)
gr.Button("๐งน Clear").click(lambda: ("", [], ""), None, [gq, gchoices, gcorrect])
with gr.Tab("๐ Smart Tutor"):
sq = gr.Textbox(label="Question")
satt = gr.Textbox(label="What did you try?")
smode = gr.Radio(["๐ก Just a Hint", "โ
Check My Work", "๐งญ Walk Me Through It", "๐ Try a Similar Problem", "๐ Explain the Concept"], label="Mode")
sconcept = gr.Textbox(label="Concept")
sgrade = gr.Dropdown(["3rd Grade", "4th Grade", "5th Grade", "6th Grade", "7th Grade"], label="Grade", value="7th Grade")
sres = gr.Textbox(label="Tutor Response")
gr.Button("๐ง Tutor Me").click(smart_tutor, [sq, satt, smode, sconcept, sgrade], sres)
gr.Button("๐งน Clear").click(lambda: ("", "", None, "", "", ""), None, [sq, satt, smode, sconcept, sgrade, sres])
with gr.Tab("๐งช AI Lab"):
lq = gr.Textbox(label="Input")
lmode = gr.Radio(["๐ Predict Difficulty", "๐ง Classify Topic", "๐งช Create Practice Set", "๐ง Explain Confusion Point", "๐ฏ Make Similar Questions", "๐ Estimate Grade Level", "๐ข Predict Step Count", "๐งฉ Extract Keywords", "๐ซ Common Mistakes"], label="Tool")
lres = gr.Textbox(label="AI Output")
gr.Button("Run AI Tool").click(ai_lab, [lq, lmode], lres)
gr.Button("๐งน Clear").click(lambda: ("", None, ""), None, [lq, lmode, lres])
with gr.Tab("โ๏ธ Settings"):
theme = gr.Radio(["default", "soft", "compact"], label="Theme")
default_grade = gr.Dropdown(["3rd Grade", "4th Grade", "5th Grade", "6th Grade", "7th Grade"], label="Default Grade")
skills = gr.Textbox(label="Math Skills (comma separated)")
saved = gr.Textbox(label="Saved Settings")
gr.Button("๐พ Save Settings").click(save_settings, [theme, default_grade, skills], saved)
gr.Button("๐งน Clear").click(lambda: (None, None, ""), None, [theme, default_grade, skills])
app.launch()
|