Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 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 huggingface_hub import InferenceClient
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 12 |
+
|
| 13 |
+
# β
Secure API Key Input (User provides it manually)
|
| 14 |
+
HF_API_KEY = st.sidebar.text_input("Enter your Hugging Face API Key", type="password")
|
| 15 |
+
|
| 16 |
+
# β
Initialize Hugging Face API Client
|
| 17 |
+
client = InferenceClient(token=HF_API_KEY) if HF_API_KEY else None
|
| 18 |
+
|
| 19 |
+
# β
Load Local Model with Device Optimization
|
| 20 |
+
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(device)
|
| 25 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
|
| 26 |
+
|
| 27 |
+
# β
Streamlit UI Setup
|
| 28 |
+
st.set_page_config(page_title="AI-Powered Timetable", layout="wide")
|
| 29 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π
AI-Powered Timetable</h1>", unsafe_allow_html=True)
|
| 30 |
+
|
| 31 |
+
# β
File Upload Section
|
| 32 |
+
st.sidebar.markdown("## π Upload Your Timetable Files")
|
| 33 |
+
uploaded_master = st.sidebar.file_uploader("Upload Master Timetable", type=["xlsx"])
|
| 34 |
+
uploaded_lab = st.sidebar.file_uploader("Upload Lab Timetable", type=["xlsx"])
|
| 35 |
+
uploaded_classroom = st.sidebar.file_uploader("Upload Classroom Timetable", type=["xlsx"])
|
| 36 |
+
uploaded_individual = st.sidebar.file_uploader("Upload Individual Timetable", type=["xlsx"])
|
| 37 |
+
|
| 38 |
+
uploaded_files = {
|
| 39 |
+
"Master Timetable": uploaded_master,
|
| 40 |
+
"Lab Timetable": uploaded_lab,
|
| 41 |
+
"Classroom Timetable": uploaded_classroom,
|
| 42 |
+
"Individual Timetable": uploaded_individual,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# β
Load Timetable Data (Directly from Uploaded File)
|
| 46 |
+
def load_timetable(file):
|
| 47 |
+
if not file:
|
| 48 |
+
return None
|
| 49 |
+
df = pd.read_excel(file)
|
| 50 |
+
return df
|
| 51 |
+
|
| 52 |
+
# β
Ask Mistral AI via API with Timetable Context
|
| 53 |
+
def ask_mistral_api_with_timetable(query, timetable_df):
|
| 54 |
+
if timetable_df is None:
|
| 55 |
+
return "No timetable uploaded."
|
| 56 |
+
|
| 57 |
+
# Convert timetable to text format
|
| 58 |
+
timetable_text = timetable_df.to_string(index=False)
|
| 59 |
+
|
| 60 |
+
prompt = f"Here is a timetable:\n\n{timetable_text}\n\nUser Query: {query}\n\nAnswer based on the timetable."
|
| 61 |
+
|
| 62 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 63 |
+
payload = {
|
| 64 |
+
"model": MODEL_NAME,
|
| 65 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 66 |
+
"max_tokens": 500,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
response = requests.post("https://api-inference.huggingface.co/v1/chat/completions", json=payload, headers=headers)
|
| 70 |
+
|
| 71 |
+
if response.status_code == 200:
|
| 72 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 73 |
+
else:
|
| 74 |
+
return f"Error: {response.json()}"
|
| 75 |
+
|
| 76 |
+
# β
AI Query Section (with Timetable Scanning)
|
| 77 |
+
st.markdown("## π€ Ask Mistral AI About Your Timetable")
|
| 78 |
+
selected_file = st.selectbox("Choose a timetable file to scan:", list(uploaded_files.keys()))
|
| 79 |
+
|
| 80 |
+
if uploaded_files[selected_file]:
|
| 81 |
+
timetable_df = load_timetable(uploaded_files[selected_file])
|
| 82 |
+
user_query = st.text_input("Type your question (e.g., 'Who is free at 10 AM on Monday?')")
|
| 83 |
+
|
| 84 |
+
if st.button("Ask AI via API with Timetable"):
|
| 85 |
+
ai_response = ask_mistral_api_with_timetable(user_query, timetable_df)
|
| 86 |
+
st.write("π§ **Mistral AI Suggests:**", ai_response)
|
| 87 |
+
else:
|
| 88 |
+
st.write("β οΈ Please upload a timetable file first.")
|
| 89 |
+
|
| 90 |
+
# β
Display Uploaded Timetables
|
| 91 |
+
st.markdown("## π View Uploaded Timetables")
|
| 92 |
+
|
| 93 |
+
for name, file in uploaded_files.items():
|
| 94 |
+
if file:
|
| 95 |
+
df = pd.read_excel(file)
|
| 96 |
+
st.markdown(f"### {name}")
|
| 97 |
+
st.dataframe(df)
|
| 98 |
+
|