timee / app.py
ShahbazAhmad-Lab's picture
Update app.py
e2d1297 verified
import sys
import types
# Fix for Python 3.13 audioop issue
sys.modules['audioop'] = types.ModuleType('audioop')
import os
import sqlite3
import gradio as gr
from groq import Groq
# Groq client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Database
conn = sqlite3.connect("timetable.db", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS timetable (
id INTEGER PRIMARY KEY AUTOINCREMENT,
day TEXT,
time TEXT,
subject TEXT,
teacher TEXT,
room TEXT
)
""")
conn.commit()
# Add class
def add_class(day, time, subject, teacher, room):
cursor.execute(
"SELECT * FROM timetable WHERE day=? AND time=? AND teacher=?",
(day, time, teacher),
)
if cursor.fetchone():
return "❌ Conflict: Teacher already assigned"
cursor.execute(
"INSERT INTO timetable (day, time, subject, teacher, room) VALUES (?, ?, ?, ?, ?)",
(day, time, subject, teacher, room),
)
conn.commit()
return "βœ… Added successfully"
# View timetable
def view_timetable():
cursor.execute("SELECT day, time, subject, teacher, room FROM timetable")
rows = cursor.fetchall()
if not rows:
return "No timetable yet"
return "\n".join(
[f"{r[0]} | {r[1]} | {r[2]} | {r[3]} | {r[4]}" for r in rows]
)
# AI assistant
def ai_assistant(prompt):
cursor.execute("SELECT day, time, subject, teacher, room FROM timetable")
data = cursor.fetchall()
context = "Timetable:\n"
for r in data:
context += f"{r[0]} {r[1]} {r[2]} {r[3]} {r[4]}\n"
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a timetable assistant."},
{"role": "user", "content": context + "\nUser: " + prompt},
],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content
# UI
with gr.Blocks() as demo:
gr.Markdown("# πŸ“… AI Timetable System")
with gr.Tab("Manage"):
day = gr.Textbox(label="Day")
time = gr.Textbox(label="Time")
subject = gr.Textbox(label="Subject")
teacher = gr.Textbox(label="Teacher")
room = gr.Textbox(label="Room")
output = gr.Textbox(label="Output")
gr.Button("Add").click(add_class, [day, time, subject, teacher, room], output)
gr.Button("View").click(view_timetable, None, output)
with gr.Tab("AI πŸ€–"):
question = gr.Textbox(label="Ask AI")
answer = gr.Textbox(label="Response")
gr.Button("Ask").click(ai_assistant, question, answer)
demo.launch()