Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import openai
|
| 4 |
import gradio as gr
|
| 5 |
-
from openai import OpenAI
|
| 6 |
from PIL import Image
|
| 7 |
import pytesseract
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# --- AI Features ---
|
| 13 |
def generate_lesson_plan(subject, grade, image=None):
|
| 14 |
image_text = extract_text(image) if image else ""
|
| 15 |
prompt = f"Create a detailed lesson plan for {subject} at grade {grade} level.\n\n{image_text}"
|
| 16 |
-
response =
|
| 17 |
-
model="
|
| 18 |
-
|
|
|
|
| 19 |
)
|
| 20 |
-
return response.choices[0].
|
| 21 |
|
| 22 |
def grade_student_answer(question, student_answer, image=None):
|
| 23 |
image_text = extract_text(image) if image else ""
|
| 24 |
prompt = f"Question: {question}\nStudent's Answer: {student_answer}\n\n{image_text}\n\nGrade this answer and provide feedback."
|
| 25 |
-
response =
|
| 26 |
-
model="
|
| 27 |
-
|
|
|
|
| 28 |
)
|
| 29 |
-
return response.choices[0].
|
| 30 |
|
| 31 |
def track_progress(notes, image=None):
|
| 32 |
image_text = extract_text(image) if image else ""
|
| 33 |
prompt = f"Summarize and analyze the following student progress notes:\n{notes}\n\n{image_text}"
|
| 34 |
-
response =
|
| 35 |
-
model="
|
| 36 |
-
|
|
|
|
| 37 |
)
|
| 38 |
-
return response.choices[0].
|
| 39 |
|
| 40 |
def extract_text_from_image(image):
|
| 41 |
text = pytesseract.image_to_string(image)
|
| 42 |
prompt = f"Extracted text from image:\n{text}\n\nProvide educational insight or summary."
|
| 43 |
-
response =
|
| 44 |
-
model="
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return response.choices[0].message.content.strip()
|
| 48 |
-
|
| 49 |
-
def generate_quiz(subject, grade, image=None):
|
| 50 |
-
image_text = extract_text(image) if image else ""
|
| 51 |
-
prompt = f"Create a 5-question multiple choice quiz for {subject} at grade {grade} level.\n\n{image_text}"
|
| 52 |
-
response = client.chat.completions.create(
|
| 53 |
-
model="gpt-3.5-turbo",
|
| 54 |
-
messages=[{"role": "user", "content": prompt}]
|
| 55 |
)
|
| 56 |
-
return response.choices[0].
|
| 57 |
-
|
| 58 |
-
def assign_homework(subject, grade, task_description, image=None):
|
| 59 |
-
image_text = extract_text(image) if image else ""
|
| 60 |
-
prompt = f"Generate a detailed homework assignment for {subject}, grade {grade}.\nTask: {task_description}\n\n{image_text}"
|
| 61 |
-
response = client.chat.completions.create(
|
| 62 |
-
model="gpt-3.5-turbo",
|
| 63 |
-
messages=[{"role": "user", "content": prompt}]
|
| 64 |
-
)
|
| 65 |
-
return response.choices[0].message.content.strip()
|
| 66 |
|
| 67 |
def extract_text(image):
|
| 68 |
if image is not None:
|
|
@@ -70,7 +57,7 @@ def extract_text(image):
|
|
| 70 |
return ""
|
| 71 |
|
| 72 |
# --- Gradio Interface ---
|
| 73 |
-
with gr.Blocks(
|
| 74 |
gr.Markdown("## π©βπ« Teacher's AI Assistant")
|
| 75 |
|
| 76 |
with gr.Tabs():
|
|
@@ -103,21 +90,5 @@ with gr.Blocks(title="Teacher AI Assistant") as app:
|
|
| 103 |
image_output = gr.Textbox(label="AI Response from Image", lines=12)
|
| 104 |
img_btn.click(extract_text_from_image, image_input, image_output)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
quiz_subject = gr.Dropdown(choices=["Math", "Science", "English", "History"], label="Subject")
|
| 108 |
-
quiz_grade = gr.Dropdown(choices=[str(i) for i in range(1, 13)], label="Grade Level")
|
| 109 |
-
quiz_image = gr.Image(type="pil", label="Optional: Upload material")
|
| 110 |
-
quiz_btn = gr.Button("Create Quiz")
|
| 111 |
-
quiz_output = gr.Textbox(label="Quiz", lines=12)
|
| 112 |
-
quiz_btn.click(generate_quiz, [quiz_subject, quiz_grade, quiz_image], quiz_output)
|
| 113 |
-
|
| 114 |
-
with gr.TabItem("π Assign Homework"):
|
| 115 |
-
hw_subject = gr.Dropdown(choices=["Math", "Science", "English", "History"], label="Subject")
|
| 116 |
-
hw_grade = gr.Dropdown(choices=[str(i) for i in range(1, 13)], label="Grade Level")
|
| 117 |
-
task_description = gr.Textbox(label="Describe the homework task")
|
| 118 |
-
hw_image = gr.Image(type="pil", label="Optional: Upload extra materials")
|
| 119 |
-
hw_btn = gr.Button("Generate Assignment")
|
| 120 |
-
hw_output = gr.Textbox(label="Homework Details", lines=10)
|
| 121 |
-
hw_btn.click(assign_homework, [hw_subject, hw_grade, task_description, hw_image], hw_output)
|
| 122 |
|
| 123 |
-
app.launch(debug=True, share=True)
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import pytesseract
|
| 6 |
|
| 7 |
+
# Access the OpenAI API key from Hugging Face Secrets
|
| 8 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Set the OpenAI API key
|
| 11 |
+
openai.api_key = api_key
|
| 12 |
|
| 13 |
# --- AI Features ---
|
| 14 |
def generate_lesson_plan(subject, grade, image=None):
|
| 15 |
image_text = extract_text(image) if image else ""
|
| 16 |
prompt = f"Create a detailed lesson plan for {subject} at grade {grade} level.\n\n{image_text}"
|
| 17 |
+
response = openai.Completion.create(
|
| 18 |
+
model="text-davinci-003",
|
| 19 |
+
prompt=prompt,
|
| 20 |
+
max_tokens=500
|
| 21 |
)
|
| 22 |
+
return response.choices[0].text.strip()
|
| 23 |
|
| 24 |
def grade_student_answer(question, student_answer, image=None):
|
| 25 |
image_text = extract_text(image) if image else ""
|
| 26 |
prompt = f"Question: {question}\nStudent's Answer: {student_answer}\n\n{image_text}\n\nGrade this answer and provide feedback."
|
| 27 |
+
response = openai.Completion.create(
|
| 28 |
+
model="text-davinci-003",
|
| 29 |
+
prompt=prompt,
|
| 30 |
+
max_tokens=500
|
| 31 |
)
|
| 32 |
+
return response.choices[0].text.strip()
|
| 33 |
|
| 34 |
def track_progress(notes, image=None):
|
| 35 |
image_text = extract_text(image) if image else ""
|
| 36 |
prompt = f"Summarize and analyze the following student progress notes:\n{notes}\n\n{image_text}"
|
| 37 |
+
response = openai.Completion.create(
|
| 38 |
+
model="text-davinci-003",
|
| 39 |
+
prompt=prompt,
|
| 40 |
+
max_tokens=500
|
| 41 |
)
|
| 42 |
+
return response.choices[0].text.strip()
|
| 43 |
|
| 44 |
def extract_text_from_image(image):
|
| 45 |
text = pytesseract.image_to_string(image)
|
| 46 |
prompt = f"Extracted text from image:\n{text}\n\nProvide educational insight or summary."
|
| 47 |
+
response = openai.Completion.create(
|
| 48 |
+
model="text-davinci-003",
|
| 49 |
+
prompt=prompt,
|
| 50 |
+
max_tokens=500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
+
return response.choices[0].text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def extract_text(image):
|
| 55 |
if image is not None:
|
|
|
|
| 57 |
return ""
|
| 58 |
|
| 59 |
# --- Gradio Interface ---
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
gr.Markdown("## π©βπ« Teacher's AI Assistant")
|
| 62 |
|
| 63 |
with gr.Tabs():
|
|
|
|
| 90 |
image_output = gr.Textbox(label="AI Response from Image", lines=12)
|
| 91 |
img_btn.click(extract_text_from_image, image_input, image_output)
|
| 92 |
|
| 93 |
+
demo.launch(debug=True, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
|
|