Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,72 @@
|
|
| 1 |
-
|
| 2 |
-
from dotenv import load_dotenv # Import dotenv
|
| 3 |
import streamlit as st
|
| 4 |
from groq import Groq
|
| 5 |
from fpdf import FPDF
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Retrieve the API key from the environment variable
|
| 11 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
else:
|
| 17 |
-
# Initialize Groq client if API key exists
|
| 18 |
-
client = Groq(api_key=groq_api_key)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cimport os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from groq import Groq
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
+
# Set up Groq API client
|
|
|
|
|
|
|
|
|
|
| 7 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
+
client = Groq(api_key=groq_api_key)
|
| 9 |
+
|
| 10 |
+
# Initialize Streamlit app
|
| 11 |
+
st.title("Timetable Generator")
|
| 12 |
+
|
| 13 |
+
# Step 1: Collect user inputs for timetable generation
|
| 14 |
+
subjects = ['Mathematics', 'Chemistry', 'Science', 'Engineering']
|
| 15 |
+
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 16 |
+
|
| 17 |
+
num_days = st.slider('Select the number of days for your timetable', 1, 7, 5)
|
| 18 |
+
time_slots = st.slider('Select the number of time slots per day', 1, 6, 4)
|
| 19 |
+
|
| 20 |
+
# User input for subjects and time slots
|
| 21 |
+
subject_schedule = {day: {subject: [] for subject in subjects} for day in days_of_week[:num_days]}
|
| 22 |
+
|
| 23 |
+
for day in days_of_week[:num_days]:
|
| 24 |
+
st.subheader(f"Schedule for {day}")
|
| 25 |
+
for subject in subjects:
|
| 26 |
+
subject_schedule[day][subject] = st.multiselect(f"Choose time slots for {subject} on {day}",
|
| 27 |
+
options=[f"{hour}:00" for hour in range(8, 8 + time_slots)],
|
| 28 |
+
label_visibility="collapsed")
|
| 29 |
|
| 30 |
+
# Step 2: Request timetable generation from Groq API
|
| 31 |
+
if st.button("Generate Timetable"):
|
| 32 |
+
timetable_input = f"Create a timetable for {subject_schedule}. The timetable should span {num_days} days and include the following subjects: {', '.join(subjects)}."
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# API call to Groq's Llama model
|
| 35 |
+
chat_completion = client.chat.completions.create(
|
| 36 |
+
messages=[{"role": "user", "content": timetable_input}],
|
| 37 |
+
model="llama-3.3-70b-versatile"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Extract generated timetable
|
| 41 |
+
generated_timetable = chat_completion.choices[0].message.content
|
| 42 |
+
st.write(generated_timetable)
|
| 43 |
|
| 44 |
+
# Step 3: Generate a PDF with the timetable
|
| 45 |
+
pdf = FPDF()
|
| 46 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 47 |
+
pdf.add_page()
|
| 48 |
|
| 49 |
+
# Set Title
|
| 50 |
+
pdf.set_font("Arial", size=12, style='B')
|
| 51 |
+
pdf.cell(200, 10, txt="Weekly Timetable", ln=True, align='C')
|
| 52 |
|
| 53 |
+
# Add each day's schedule to the PDF
|
| 54 |
+
pdf.set_font("Arial", size=10)
|
| 55 |
+
for day in subject_schedule:
|
| 56 |
+
pdf.ln(10)
|
| 57 |
+
pdf.cell(200, 10, txt=day, ln=True, align='L')
|
| 58 |
+
for subject in subject_schedule[day]:
|
| 59 |
+
time_slots = ", ".join(subject_schedule[day][subject])
|
| 60 |
+
pdf.cell(200, 10, txt=f"{subject}: {time_slots}", ln=True)
|
| 61 |
|
| 62 |
+
# Save the PDF
|
| 63 |
+
pdf.output("timetable.pdf")
|
| 64 |
|
| 65 |
+
# Step 4: Allow user to download the PDF
|
| 66 |
+
with open("timetable.pdf", "rb") as f:
|
| 67 |
+
st.download_button(
|
| 68 |
+
label="Download Timetable as PDF",
|
| 69 |
+
data=f,
|
| 70 |
+
file_name="timetable.pdf",
|
| 71 |
+
mime="application/pdf"
|
| 72 |
+
)
|