Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,153 +1,111 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
import openpyxl
|
| 4 |
-
import
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
-
from reportlab.pdfgen import canvas
|
| 7 |
from reportlab.lib.pagesizes import letter
|
| 8 |
-
from
|
| 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 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# PDF Download Option
|
| 115 |
-
st.download_button(
|
| 116 |
-
label=f"π₯ Download {file_name} as PDF",
|
| 117 |
-
data=save_pdf(text).getvalue(),
|
| 118 |
-
file_name=f"{file_name}.pdf",
|
| 119 |
-
mime="application/pdf"
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
for file_name, df in edited_excels.items():
|
| 123 |
-
output = BytesIO()
|
| 124 |
-
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
| 125 |
-
df.to_excel(writer, index=False, sheet_name="EditedSheet")
|
| 126 |
-
writer.close()
|
| 127 |
-
|
| 128 |
-
st.download_button(
|
| 129 |
-
label=f"π₯ Download {file_name}",
|
| 130 |
-
data=output.getvalue(),
|
| 131 |
-
file_name=file_name,
|
| 132 |
-
mime="application/octet-stream" # FIXED MIME TYPE
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
# π¨οΈ Chat with TinyLlama about Timetable Conflicts
|
| 136 |
-
st.subheader("ποΈ Timetable Conflict Checker")
|
| 137 |
-
query = st.text_input("Ask about conflicts in the uploaded Timetables:")
|
| 138 |
-
|
| 139 |
-
if query:
|
| 140 |
-
conflict_info = "No conflicts detected." # Default response
|
| 141 |
-
for file_key, file in uploaded_files.items():
|
| 142 |
-
if file and file.name.endswith(".xls"):
|
| 143 |
-
xls = load_excel(file)
|
| 144 |
-
for sheet in xls.sheet_names:
|
| 145 |
-
df = pd.read_excel(xls, sheet_name=sheet)
|
| 146 |
-
if "Monday" in df.columns: # Adjust this based on column headers
|
| 147 |
-
monday_data = df["Monday"].dropna().astype(str).tolist()
|
| 148 |
-
response = generate_suggestion(f"Analyze this schedule: {monday_data}. Find conflicts.")
|
| 149 |
-
if "conflict" in response.lower():
|
| 150 |
-
conflict_info = response
|
| 151 |
-
|
| 152 |
-
st.write("π’ **Conflict Analysis:**")
|
| 153 |
-
st.write(conflict_info)
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
import openpyxl
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import streamlit as st
|
| 6 |
from io import BytesIO
|
|
|
|
| 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")
|
| 12 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π
AI-Powered Timetable</h1>", unsafe_allow_html=True)
|
| 13 |
+
|
| 14 |
+
# β
API Key Input
|
| 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 |
+
uploaded_file = st.sidebar.file_uploader("Upload Timetable", type=["xlsx"])
|
| 21 |
+
|
| 22 |
+
# β
Load Timetable Data
|
| 23 |
+
def load_timetable(file):
|
| 24 |
+
if file:
|
| 25 |
+
df = pd.read_excel(file)
|
| 26 |
+
return df
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
df = load_timetable(uploaded_file)
|
| 30 |
+
|
| 31 |
+
# β
Ask TinyLlama API
|
| 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:
|
| 46 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 47 |
+
else:
|
| 48 |
+
return f"API Error: {response.status_code} - {response.text}"
|
| 49 |
+
|
| 50 |
+
# β
Auto-Schedule Missing Slots
|
| 51 |
+
def auto_schedule(df):
|
| 52 |
+
if df is None:
|
| 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 |
+
y_position = 730
|
| 77 |
+
for i, row in df.iterrows():
|
| 78 |
+
pdf.drawString(30, y_position, str(row.values))
|
| 79 |
+
y_position -= 20
|
| 80 |
+
if y_position < 50:
|
| 81 |
+
pdf.showPage()
|
| 82 |
+
pdf.setFont("Helvetica", 10)
|
| 83 |
+
y_position = 750
|
| 84 |
+
|
| 85 |
+
pdf.save()
|
| 86 |
+
buffer.seek(0)
|
| 87 |
+
return buffer
|
| 88 |
+
|
| 89 |
+
# β
Display and Edit Timetable
|
| 90 |
+
if df is not None:
|
| 91 |
+
st.markdown("## π Edit Your Timetable")
|
| 92 |
+
edited_df = st.data_editor(df, key="editable_table")
|
| 93 |
+
|
| 94 |
+
# β
Auto-Schedule Button
|
| 95 |
+
if st.button("Auto-Schedule Missing Slots"):
|
| 96 |
+
result = auto_schedule(edited_df)
|
| 97 |
+
st.write("β
", result)
|
| 98 |
+
|
| 99 |
+
# β
Download as PDF
|
| 100 |
+
if st.button("Download as PDF"):
|
| 101 |
+
pdf_buffer = convert_to_pdf(edited_df)
|
| 102 |
+
st.download_button("π₯ Download Timetable PDF", pdf_buffer, file_name="timetable.pdf", mime="application/pdf")
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|