Spaces:
Sleeping
Sleeping
File size: 2,629 Bytes
c2e2fbb e2d1297 36ef6eb e2d1297 36ef6eb e2d1297 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb e2d1297 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb 8dd4182 36ef6eb | 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 | 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() |