Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import datetime
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# π Setup API Key (store as HF_SECRET)
|
| 8 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
client = OpenAI(api_key=openai_api_key)
|
| 10 |
+
|
| 11 |
+
def generate_plan(subjects, exam_date, hours_per_day):
|
| 12 |
+
subjects = [s.strip() for s in subjects.split(",")]
|
| 13 |
+
exam_date = pd.to_datetime(exam_date)
|
| 14 |
+
today = pd.to_datetime("today").normalize()
|
| 15 |
+
total_days = (exam_date - today).days
|
| 16 |
+
|
| 17 |
+
if total_days <= 0 or not subjects:
|
| 18 |
+
return "Invalid input", None, None
|
| 19 |
+
|
| 20 |
+
total_hours = total_days * hours_per_day
|
| 21 |
+
hours_per_subject = total_hours // len(subjects)
|
| 22 |
+
|
| 23 |
+
# Simple schedule
|
| 24 |
+
schedule = []
|
| 25 |
+
for i in range(total_days):
|
| 26 |
+
date = today + pd.Timedelta(days=i)
|
| 27 |
+
subject = subjects[i % len(subjects)]
|
| 28 |
+
schedule.append({
|
| 29 |
+
"Date": date.date(),
|
| 30 |
+
"Subject": subject,
|
| 31 |
+
"Hours": round(hours_per_day / len(subjects), 2)
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
df = pd.DataFrame(schedule)
|
| 35 |
+
|
| 36 |
+
# Generate Tip from LLM
|
| 37 |
+
prompt = f"""Generate a short, motivating study tip for a student studying {', '.join(subjects)} with {hours_per_day} hours per day until {exam_date.date()}."""
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model="gpt-4",
|
| 40 |
+
messages=[{"role": "user", "content": prompt}]
|
| 41 |
+
)
|
| 42 |
+
tip = response.choices[0].message.content.strip()
|
| 43 |
+
|
| 44 |
+
return tip, df, df.to_csv(index=False)
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## π Personalized Study Plan Generator")
|
| 49 |
+
|
| 50 |
+
subjects = gr.Textbox(label="Enter subjects (comma-separated)")
|
| 51 |
+
exam_date = gr.Textbox(label="Enter exam date (YYYY-MM-DD)")
|
| 52 |
+
hours = gr.Slider(1, 12, step=1, label="Hours per day")
|
| 53 |
+
btn = gr.Button("Generate Plan")
|
| 54 |
+
|
| 55 |
+
output_tip = gr.Textbox(label="AI Study Tip")
|
| 56 |
+
output_table = gr.Dataframe(label="Study Plan")
|
| 57 |
+
download_csv = gr.File(label="Download CSV")
|
| 58 |
+
|
| 59 |
+
def generate_and_show(subjects, exam_date, hours):
|
| 60 |
+
tip, df, csv = generate_plan(subjects, exam_date, hours)
|
| 61 |
+
with open("study_plan.csv", "w") as f:
|
| 62 |
+
f.write(csv)
|
| 63 |
+
return tip, df, "study_plan.csv"
|
| 64 |
+
|
| 65 |
+
btn.click(fn=generate_and_show, inputs=[subjects, exam_date, hours],
|
| 66 |
+
outputs=[output_tip, output_table, download_csv])
|
| 67 |
+
|
| 68 |
+
demo.launch()
|