styx102's picture
Update app.py
b7630be verified
import gradio as gr
import pdfplumber
import docx
import os
import openai
client = openai.OpenAI() # Uses OPENAI_API_KEY from environment
def extract_text_from_file(file):
if file.name.endswith(".pdf"):
with pdfplumber.open(file.name) as pdf:
return "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
elif file.name.endswith(".docx"):
doc = docx.Document(file.name)
return "\n".join([p.text for p in doc.paragraphs])
elif file.name.endswith(".txt"):
return file.read().decode("utf-8")
else:
return "Unsupported file type."
def generate_content(tek_text):
prompt = f"""
You are an AI education assistant. The following is a list of TEKS (Texas Essential Knowledge and Skills) or similar learning standards:
{tek_text}
For each TEK or standard, generate:
1. A short summary or note explaining the concept.
2. A list of 3-5 important vocabulary words.
3. 2-3 practice problems (multiple choice, fill in the blank, or short answer).
Format:
TEK: <actual text>
Notes: <summary>
Vocabulary: <words>
Practice Problems:
1. <question>
2. <question>
---
"""
response = client.chat.completions.create(
model="gpt-3",
messages=[
{"role": "system", "content": "You are a helpful AI educator."},
{"role": "user", "content": prompt}
],
temperature=0.7
)
return response.choices[0].message.content
def process_file(file):
teks_text = extract_text_from_file(file)
if teks_text.startswith("Unsupported"):
return teks_text
return generate_content(teks_text)
with gr.Blocks() as demo:
gr.Markdown("# ๐Ÿ“š TEKS Learning Generator\\nUpload a TEKS or learning standard document (PDF, DOCX, or TXT), and the AI will generate notes, vocabulary, and practice problems for each TEK.")
with gr.Row():
file_input = gr.File(label="Upload TEKS Document")
output = gr.Textbox(label="AI-Generated Output", lines=30)
submit_btn = gr.Button("Generate Learning Content")
submit_btn.click(fn=process_file, inputs=file_input, outputs=output)
if __name__ == "__main__":
demo.launch()