Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
-
import pandas as pd
|
| 5 |
import streamlit as st
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
from reportlab.lib.pagesizes import letter
|
| 8 |
from reportlab.pdfgen import canvas
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# β
Streamlit UI Setup
|
| 11 |
st.set_page_config(page_title="AI-Powered Timetable", layout="wide")
|
|
@@ -15,31 +19,35 @@ st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π
AI-Powered Tim
|
|
| 15 |
st.sidebar.markdown("## π Enter Hugging Face API Key")
|
| 16 |
hf_api_key = st.sidebar.text_input("API Key", type="password")
|
| 17 |
|
| 18 |
-
# β
File Upload Section
|
| 19 |
st.sidebar.markdown("## π Upload Your Timetable Files")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def load_timetable(file):
|
| 24 |
-
if file:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
def ask_tinyllama_api(query):
|
| 33 |
if not hf_api_key:
|
| 34 |
return "Error: Please enter your API key."
|
| 35 |
|
| 36 |
url = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 37 |
headers = {"Authorization": f"Bearer {hf_api_key}", "Content-Type": "application/json"}
|
| 38 |
-
payload = {
|
| 39 |
-
"model": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 40 |
-
"messages": [{"role": "user", "content": query}],
|
| 41 |
-
"max_tokens": 500
|
| 42 |
-
}
|
| 43 |
|
| 44 |
response = requests.post(url, headers=headers, json=payload)
|
| 45 |
if response.status_code == 200:
|
|
@@ -47,65 +55,100 @@ def ask_tinyllama_api(query):
|
|
| 47 |
else:
|
| 48 |
return f"API Error: {response.status_code} - {response.text}"
|
| 49 |
|
| 50 |
-
# β
Auto-Schedule Missing Slots
|
| 51 |
-
def auto_schedule(
|
| 52 |
-
if
|
| 53 |
return "No timetable uploaded."
|
| 54 |
-
|
| 55 |
-
empty_slots = df[df.isnull().any(axis=1)].index.tolist()
|
| 56 |
-
for idx in empty_slots:
|
| 57 |
-
query = f"Suggest a subject and faculty for the empty slot in row {idx+2}."
|
| 58 |
-
suggestion = ask_tinyllama_api(query)
|
| 59 |
-
try:
|
| 60 |
-
subject, faculty = suggestion.split(", Faculty: ")
|
| 61 |
-
df.at[idx, "Subject"] = subject.strip()
|
| 62 |
-
df.at[idx, "Faculty"] = faculty.strip()
|
| 63 |
-
except:
|
| 64 |
-
continue
|
| 65 |
-
|
| 66 |
-
return f"Auto-scheduling completed for {len(empty_slots)} slots."
|
| 67 |
-
|
| 68 |
-
# β
Convert DataFrame to PDF
|
| 69 |
-
def convert_to_pdf(df):
|
| 70 |
-
buffer = BytesIO()
|
| 71 |
-
pdf = canvas.Canvas(buffer, pagesize=letter)
|
| 72 |
-
pdf.setFont("Helvetica", 10)
|
| 73 |
-
|
| 74 |
-
pdf.drawString(30, 750, "AI-Powered Timetable")
|
| 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 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# β
AI Query Section
|
| 105 |
-
st.markdown("## π€ Ask TinyLlama About Your Timetable")
|
| 106 |
user_query = st.text_input("Type your question here (e.g., 'Who is free at 10 AM on Monday?')")
|
| 107 |
|
| 108 |
if st.button("Ask AI via API"):
|
| 109 |
ai_response = ask_tinyllama_api(user_query)
|
| 110 |
st.write("π§ **TinyLlama Suggests:**", ai_response)
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
+
import os
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import openpyxl
|
| 7 |
+
import torch
|
| 8 |
from reportlab.lib.pagesizes import letter
|
| 9 |
from reportlab.pdfgen import canvas
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
from docx import Document
|
| 12 |
+
from io import BytesIO
|
| 13 |
|
| 14 |
# β
Streamlit UI Setup
|
| 15 |
st.set_page_config(page_title="AI-Powered Timetable", layout="wide")
|
|
|
|
| 19 |
st.sidebar.markdown("## π Enter Hugging Face API Key")
|
| 20 |
hf_api_key = st.sidebar.text_input("API Key", type="password")
|
| 21 |
|
| 22 |
+
# β
File Upload Section (Excel & DOCX)
|
| 23 |
st.sidebar.markdown("## π Upload Your Timetable Files")
|
| 24 |
+
uploaded_files = {
|
| 25 |
+
"Master Timetable": st.sidebar.file_uploader("Upload Master Timetable", type=["xlsx", "docx"]),
|
| 26 |
+
"Lab Timetable": st.sidebar.file_uploader("Upload Lab Timetable", type=["xlsx", "docx"]),
|
| 27 |
+
"Classroom Timetable": st.sidebar.file_uploader("Upload Classroom Timetable", type=["xlsx", "docx"]),
|
| 28 |
+
"Individual Timetable": st.sidebar.file_uploader("Upload Individual Timetable", type=["xlsx", "docx"]),
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# β
Load Timetable Data (Excel & DOCX)
|
| 32 |
def load_timetable(file):
|
| 33 |
+
if not file:
|
| 34 |
+
return None
|
| 35 |
+
if file.name.endswith(".xlsx"):
|
| 36 |
+
wb = openpyxl.load_workbook(file)
|
| 37 |
+
sheet = wb.active
|
| 38 |
+
return [row for row in sheet.iter_rows(values_only=True)]
|
| 39 |
+
elif file.name.endswith(".docx"):
|
| 40 |
+
doc = Document(file)
|
| 41 |
+
return [para.text for para in doc.paragraphs]
|
| 42 |
+
|
| 43 |
+
# β
Ask TinyLlama AI via API
|
| 44 |
def ask_tinyllama_api(query):
|
| 45 |
if not hf_api_key:
|
| 46 |
return "Error: Please enter your API key."
|
| 47 |
|
| 48 |
url = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 49 |
headers = {"Authorization": f"Bearer {hf_api_key}", "Content-Type": "application/json"}
|
| 50 |
+
payload = {"model": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "messages": [{"role": "user", "content": query}], "max_tokens": 500}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
response = requests.post(url, headers=headers, json=payload)
|
| 53 |
if response.status_code == 200:
|
|
|
|
| 55 |
else:
|
| 56 |
return f"API Error: {response.status_code} - {response.text}"
|
| 57 |
|
| 58 |
+
# β
Auto-Schedule Missing Slots (Excel & DOCX)
|
| 59 |
+
def auto_schedule(file):
|
| 60 |
+
if not file:
|
| 61 |
return "No timetable uploaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
if file.name.endswith(".xlsx"):
|
| 64 |
+
wb = openpyxl.load_workbook(file)
|
| 65 |
+
sheet = wb.active
|
| 66 |
+
|
| 67 |
+
empty_slots = []
|
| 68 |
+
for row_idx, row in enumerate(sheet.iter_rows(min_row=2, values_only=True), start=2):
|
| 69 |
+
if None in row or "" in row:
|
| 70 |
+
empty_slots.append(row_idx)
|
| 71 |
+
|
| 72 |
+
for row_idx in empty_slots:
|
| 73 |
+
query = f"Suggest a subject and faculty for the empty slot in row {row_idx}."
|
| 74 |
+
suggestion = ask_tinyllama_api(query)
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
subject, faculty = suggestion.split(", Faculty: ")
|
| 78 |
+
sheet.cell(row=row_idx, column=4, value=subject.strip())
|
| 79 |
+
sheet.cell(row=row_idx, column=5, value=faculty.strip())
|
| 80 |
+
except:
|
| 81 |
+
continue
|
| 82 |
+
|
| 83 |
+
return f"Auto-scheduling completed for {len(empty_slots)} slots."
|
| 84 |
+
|
| 85 |
+
elif file.name.endswith(".docx"):
|
| 86 |
+
doc = Document(file)
|
| 87 |
+
new_doc = Document()
|
| 88 |
+
for para in doc.paragraphs:
|
| 89 |
+
if "EMPTY SLOT" in para.text:
|
| 90 |
+
query = "Suggest a subject and faculty for this slot."
|
| 91 |
+
suggestion = ask_tinyllama_api(query)
|
| 92 |
+
new_doc.add_paragraph(suggestion)
|
| 93 |
+
else:
|
| 94 |
+
new_doc.add_paragraph(para.text)
|
| 95 |
+
return new_doc
|
| 96 |
|
| 97 |
# β
AI Query Section
|
| 98 |
+
st.markdown("## π€ Ask TinyLlama AI About Your Timetable")
|
| 99 |
user_query = st.text_input("Type your question here (e.g., 'Who is free at 10 AM on Monday?')")
|
| 100 |
|
| 101 |
if st.button("Ask AI via API"):
|
| 102 |
ai_response = ask_tinyllama_api(user_query)
|
| 103 |
st.write("π§ **TinyLlama Suggests:**", ai_response)
|
| 104 |
+
|
| 105 |
+
# β
Auto-Schedule Feature
|
| 106 |
+
st.markdown("## π
Auto-Schedule Missing Timetable Slots")
|
| 107 |
+
selected_file = st.selectbox("Choose a timetable file to auto-fill missing slots:", list(uploaded_files.keys()))
|
| 108 |
+
|
| 109 |
+
if st.button("Auto-Schedule"):
|
| 110 |
+
result = auto_schedule(uploaded_files[selected_file])
|
| 111 |
+
if isinstance(result, Document): # DOCX file
|
| 112 |
+
output = BytesIO()
|
| 113 |
+
result.save(output)
|
| 114 |
+
output.seek(0)
|
| 115 |
+
st.download_button("π₯ Download Updated DOCX", output, file_name="updated_timetable.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 116 |
+
else:
|
| 117 |
+
st.write("β
", result)
|
| 118 |
+
|
| 119 |
+
# β
Display Uploaded Timetables
|
| 120 |
+
st.markdown("## π View & Edit Uploaded Timetables")
|
| 121 |
+
|
| 122 |
+
for name, file in uploaded_files.items():
|
| 123 |
+
if file:
|
| 124 |
+
if file.name.endswith(".xlsx"):
|
| 125 |
+
df = pd.read_excel(file)
|
| 126 |
+
edited_df = st.data_editor(df, num_rows="dynamic")
|
| 127 |
+
st.download_button("π₯ Download Edited Excel", edited_df.to_csv(index=False).encode(), file_name=f"{name}.csv", mime="text/csv")
|
| 128 |
+
elif file.name.endswith(".docx"):
|
| 129 |
+
doc = Document(file)
|
| 130 |
+
doc_text = "\n".join([para.text for para in doc.paragraphs])
|
| 131 |
+
edited_text = st.text_area(f"Edit {name}:", doc_text)
|
| 132 |
+
new_doc = Document()
|
| 133 |
+
for line in edited_text.split("\n"):
|
| 134 |
+
new_doc.add_paragraph(line)
|
| 135 |
+
output = BytesIO()
|
| 136 |
+
new_doc.save(output)
|
| 137 |
+
output.seek(0)
|
| 138 |
+
st.download_button("π₯ Download Edited DOCX", output, file_name=f"{name}.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 139 |
+
|
| 140 |
+
# β
PDF Export Feature
|
| 141 |
+
st.markdown("## π Export to PDF")
|
| 142 |
+
if st.button("Export to PDF"):
|
| 143 |
+
pdf_output = BytesIO()
|
| 144 |
+
c = canvas.Canvas(pdf_output, pagesize=letter)
|
| 145 |
+
c.drawString(100, 750, "AI-Powered Timetable Export")
|
| 146 |
+
y_position = 730
|
| 147 |
+
for name, file in uploaded_files.items():
|
| 148 |
+
if file:
|
| 149 |
+
c.drawString(100, y_position, f"{name}: {file.name}")
|
| 150 |
+
y_position -= 20
|
| 151 |
+
c.save()
|
| 152 |
+
pdf_output.seek(0)
|
| 153 |
+
st.download_button("π₯ Download PDF", pdf_output, file_name="timetable.pdf", mime="application/pdf")
|
| 154 |
+
|