Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
-
import os
|
| 4 |
import streamlit as st
|
| 5 |
import pandas as pd
|
| 6 |
import openpyxl
|
|
@@ -28,17 +27,24 @@ uploaded_files = {
|
|
| 28 |
"Individual Timetable": st.sidebar.file_uploader("Upload Individual Timetable", type=["xlsx", "docx"]),
|
| 29 |
}
|
| 30 |
|
| 31 |
-
# β
Load
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# β
Ask TinyLlama AI via API
|
| 44 |
def ask_tinyllama_api(query):
|
|
@@ -46,8 +52,15 @@ def ask_tinyllama_api(query):
|
|
| 46 |
return "Error: Please enter your API key."
|
| 47 |
|
| 48 |
url = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 49 |
-
headers = {
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
response = requests.post(url, headers=headers, json=payload)
|
| 53 |
if response.status_code == 200:
|
|
@@ -55,44 +68,28 @@ def ask_tinyllama_api(query):
|
|
| 55 |
else:
|
| 56 |
return f"API Error: {response.status_code} - {response.text}"
|
| 57 |
|
| 58 |
-
# β
Auto-Schedule Missing Slots
|
| 59 |
def auto_schedule(file):
|
| 60 |
if not file:
|
| 61 |
return "No timetable uploaded."
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 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")
|
|
@@ -107,37 +104,31 @@ 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 |
-
|
| 111 |
-
|
| 112 |
-
|
| 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("
|
| 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 =
|
| 126 |
edited_df = st.data_editor(df, num_rows="dynamic")
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
elif file.name.endswith(".docx"):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 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()
|
|
@@ -151,4 +142,4 @@ if st.button("Export to PDF"):
|
|
| 151 |
c.save()
|
| 152 |
pdf_output.seek(0)
|
| 153 |
st.download_button("π₯ Download PDF", pdf_output, file_name="timetable.pdf", mime="application/pdf")
|
| 154 |
-
|
|
|
|
| 1 |
import json
|
| 2 |
import requests
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import pandas as pd
|
| 5 |
import openpyxl
|
|
|
|
| 27 |
"Individual Timetable": st.sidebar.file_uploader("Upload Individual Timetable", type=["xlsx", "docx"]),
|
| 28 |
}
|
| 29 |
|
| 30 |
+
# β
Load XLSX File
|
| 31 |
+
def load_xlsx(file):
|
| 32 |
+
return pd.read_excel(file)
|
| 33 |
+
|
| 34 |
+
# β
Load DOCX File
|
| 35 |
+
def load_docx(file):
|
| 36 |
+
doc = Document(file)
|
| 37 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 38 |
+
|
| 39 |
+
# β
Save DOCX File
|
| 40 |
+
def save_docx(text):
|
| 41 |
+
doc = Document()
|
| 42 |
+
for line in text.split("\n"):
|
| 43 |
+
doc.add_paragraph(line)
|
| 44 |
+
output = BytesIO()
|
| 45 |
+
doc.save(output)
|
| 46 |
+
output.seek(0)
|
| 47 |
+
return output
|
| 48 |
|
| 49 |
# β
Ask TinyLlama AI via API
|
| 50 |
def ask_tinyllama_api(query):
|
|
|
|
| 52 |
return "Error: Please enter your API key."
|
| 53 |
|
| 54 |
url = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 55 |
+
headers = {
|
| 56 |
+
"Authorization": f"Bearer {hf_api_key}",
|
| 57 |
+
"Content-Type": "application/json"
|
| 58 |
+
}
|
| 59 |
+
payload = {
|
| 60 |
+
"model": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 61 |
+
"messages": [{"role": "user", "content": query}],
|
| 62 |
+
"max_tokens": 500
|
| 63 |
+
}
|
| 64 |
|
| 65 |
response = requests.post(url, headers=headers, json=payload)
|
| 66 |
if response.status_code == 200:
|
|
|
|
| 68 |
else:
|
| 69 |
return f"API Error: {response.status_code} - {response.text}"
|
| 70 |
|
| 71 |
+
# β
Auto-Schedule Missing Slots in Excel
|
| 72 |
def auto_schedule(file):
|
| 73 |
if not file:
|
| 74 |
return "No timetable uploaded."
|
| 75 |
|
| 76 |
+
df = pd.read_excel(file)
|
| 77 |
+
empty_slots = df[df.isnull().any(axis=1)]
|
| 78 |
+
|
| 79 |
+
for index, row in empty_slots.iterrows():
|
| 80 |
+
query = f"Suggest a subject and faculty for an empty slot on {row.get('Day', 'Unknown')} at {row.get('Time', 'Unknown')}."
|
| 81 |
+
suggestion = ask_tinyllama_api(query)
|
| 82 |
+
try:
|
| 83 |
+
subject, faculty = suggestion.split(", Faculty: ")
|
| 84 |
+
df.loc[index, "Subject"] = subject.strip()
|
| 85 |
+
df.loc[index, "Faculty"] = faculty.strip()
|
| 86 |
+
except:
|
| 87 |
+
continue
|
| 88 |
+
|
| 89 |
+
output = BytesIO()
|
| 90 |
+
df.to_excel(output, index=False, engine='openpyxl')
|
| 91 |
+
output.seek(0)
|
| 92 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# β
AI Query Section
|
| 95 |
st.markdown("## π€ Ask TinyLlama AI About Your Timetable")
|
|
|
|
| 104 |
selected_file = st.selectbox("Choose a timetable file to auto-fill missing slots:", list(uploaded_files.keys()))
|
| 105 |
|
| 106 |
if st.button("Auto-Schedule"):
|
| 107 |
+
if uploaded_files[selected_file]:
|
| 108 |
+
scheduled_file = auto_schedule(uploaded_files[selected_file])
|
| 109 |
+
st.download_button("π₯ Download Auto-Scheduled Timetable", scheduled_file, file_name=f"{selected_file}_auto.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
|
|
|
|
|
|
|
|
| 110 |
else:
|
| 111 |
+
st.write("β οΈ Please upload a file first.")
|
| 112 |
|
| 113 |
+
# β
Display & Edit Uploaded Timetables (DOCX + XLSX)
|
| 114 |
st.markdown("## π View & Edit Uploaded Timetables")
|
| 115 |
|
| 116 |
for name, file in uploaded_files.items():
|
| 117 |
if file:
|
| 118 |
if file.name.endswith(".xlsx"):
|
| 119 |
+
df = load_xlsx(file)
|
| 120 |
edited_df = st.data_editor(df, num_rows="dynamic")
|
| 121 |
+
csv_output = edited_df.to_csv(index=False).encode()
|
| 122 |
+
st.download_button("π₯ Download Edited Excel", csv_output, file_name=f"{name}.csv", mime="text/csv")
|
| 123 |
+
|
| 124 |
elif file.name.endswith(".docx"):
|
| 125 |
+
doc_text = load_docx(file)
|
| 126 |
+
edited_text = st.text_area(f"π Edit {name}:", doc_text, height=400)
|
| 127 |
+
if st.button(f"πΎ Save & Download {name}"):
|
| 128 |
+
docx_output = save_docx(edited_text)
|
| 129 |
+
st.download_button("π₯ Download Edited DOCX", docx_output, file_name=f"{name}.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 130 |
+
|
| 131 |
+
# β
Export to PDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
st.markdown("## π Export to PDF")
|
| 133 |
if st.button("Export to PDF"):
|
| 134 |
pdf_output = BytesIO()
|
|
|
|
| 142 |
c.save()
|
| 143 |
pdf_output.seek(0)
|
| 144 |
st.download_button("π₯ Download PDF", pdf_output, file_name="timetable.pdf", mime="application/pdf")
|
| 145 |
+
|