File size: 6,411 Bytes
037eef9
d68d809
037eef9
 
 
 
 
 
 
 
 
689f72f
037eef9
 
 
f97be12
 
 
 
689f72f
037eef9
 
 
 
 
 
689f72f
 
 
 
 
 
 
 
 
 
037eef9
 
 
 
 
689f72f
037eef9
f97be12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
037eef9
689f72f
 
 
037eef9
 
689f72f
037eef9
 
 
 
 
 
 
 
 
f97be12
037eef9
 
 
689f72f
 
037eef9
 
 
 
 
689f72f
037eef9
 
 
 
 
 
 
fc0d10d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f97be12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import json
import requests
import os
import streamlit as st
import pandas as pd
import openpyxl
import torch
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# βœ… Streamlit UI Setup
st.set_page_config(page_title="AI-Powered Timetable", layout="wide")
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>πŸ“… AI-Powered Timetable</h1>", unsafe_allow_html=True)

# βœ… API Key Input
st.sidebar.markdown("## πŸ”‘ Enter Hugging Face API Key")
hf_api_key = st.sidebar.text_input("API Key", type="password")

# βœ… File Upload Section
st.sidebar.markdown("## πŸ“‚ Upload Your Timetable Files")
uploaded_master = st.sidebar.file_uploader("Upload Master Timetable", type=["xlsx"])
uploaded_lab = st.sidebar.file_uploader("Upload Lab Timetable", type=["xlsx"])
uploaded_classroom = st.sidebar.file_uploader("Upload Classroom Timetable", type=["xlsx"])
uploaded_individual = st.sidebar.file_uploader("Upload Individual Timetable", type=["xlsx"])

uploaded_files = {
    "Master Timetable": uploaded_master,
    "Lab Timetable": uploaded_lab,
    "Classroom Timetable": uploaded_classroom,
    "Individual Timetable": uploaded_individual,
}

# βœ… Load Timetable Data (Directly from Uploaded File)
def load_timetable(file):
    if not file:
        return None
    wb = openpyxl.load_workbook(file)
    sheet = wb.active
    return [row for row in sheet.iter_rows(values_only=True)]

# βœ… Ask Mistral AI via API
def ask_mistral_api(query):
    if not hf_api_key:
        return "Error: Please enter your API key."

    url = "https://api-inference.huggingface.co/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {hf_api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "model": "mistralai/Mistral-7B-Instruct-v0.2",
        "messages": [{"role": "user", "content": query}],
        "max_tokens": 500
    }

    response = requests.post(url, headers=headers, json=payload)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return f"API Error: {response.status_code} - {response.text}"

# βœ… Auto-Schedule Missing Slots
def auto_schedule(file):
    if not file:
        return "No timetable uploaded."

    wb = openpyxl.load_workbook(file)
    sheet = wb.active

    empty_slots = []
    for row_idx, row in enumerate(sheet.iter_rows(min_row=2, values_only=True), start=2):
        if None in row or "" in row:
            empty_slots.append(row_idx)

    for row_idx in empty_slots:
        query = f"Suggest a subject and faculty for the empty slot in row {row_idx}."
        suggestion = ask_mistral_api(query)

        try:
            subject, faculty = suggestion.split(", Faculty: ")
            sheet.cell(row=row_idx, column=4, value=subject.strip())
            sheet.cell(row=row_idx, column=5, value=faculty.strip())
        except:
            continue  

    return f"Auto-scheduling completed for {len(empty_slots)} slots."

# βœ… AI Query Section
st.markdown("## πŸ€– Ask Mistral AI About Your Timetable")
user_query = st.text_input("Type your question here (e.g., 'Who is free at 10 AM on Monday?')")

if st.button("Ask AI via API"):
    ai_response = ask_mistral_api(user_query)
    st.write("🧠 **Mistral AI Suggests:**", ai_response)

# βœ… Auto-Schedule Feature
st.markdown("## πŸ“… Auto-Schedule Missing Timetable Slots")
selected_file = st.selectbox("Choose a timetable file to auto-fill missing slots:", list(uploaded_files.keys()))

if st.button("Auto-Schedule"):
    result = auto_schedule(uploaded_files[selected_file])
    st.write("βœ…", result)

# βœ… Display Uploaded Timetables
st.markdown("## πŸ“œ View Uploaded Timetables")

for name, file in uploaded_files.items():
    if file:
        df = pd.read_excel(file)
        st.markdown(f"### {name}")
        st.dataframe(df)

# βœ… Inject JavaScript for Real-Time Chat
st.markdown("""
    <script>
        async function fetchChatResponse() {
            const apiKey = document.getElementById("hf-api-key").value;
            const userInput = document.getElementById("user-input").value;
            if (!apiKey) {
                alert("Please enter your Hugging Face API key.");
                return;
            }

            try {
                const response = await fetch("https://api-inference.huggingface.co/v1/chat/completions", {
                    method: "POST",
                    headers: {
                        "Authorization": `Bearer ${apiKey}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        model: "mistralai/Mistral-7B-Instruct-v0.2",
                        messages: [{ role: "user", content: userInput }],
                        max_tokens: 500
                    })
                });

                if (!response.ok) {
                    throw new Error(`API Error: ${response.statusText}`);
                }

                const data = await response.json();
                const botMessage = data.choices[0].message.content;

                document.getElementById("chat-box").innerHTML += 
                    `<div class='bot-message'><strong>VarunGPT-3:</strong> ${botMessage}</div>`;

            } catch (error) {
                console.error("Error fetching chat response:", error);
                document.getElementById("chat-box").innerHTML += 
                    `<div class='bot-message'><strong>Error:</strong> Unable to fetch response.</div>`;
            }
        }
    </script>
""", unsafe_allow_html=True)

# βœ… Chat UI with User Input for API Key
st.markdown("""
    <div style="text-align: center;">
        <input id="hf-api-key" type="password" placeholder="Enter Hugging Face API Key" 
               style="width: 50%; padding: 8px; margin-bottom: 10px;"/>
        <br/>
        <input id="user-input" type="text" placeholder="Type your message..." 
               style="width: 50%; padding: 8px;"/>
        <button onclick="fetchChatResponse()" 
                style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">
            Ask AI
        </button>
        <div id="chat-box" style="margin-top: 20px; text-align: left;"></div>
    </div>
""", unsafe_allow_html=True)