Waseem7711 commited on
Commit
f2d3105
·
verified ·
1 Parent(s): 6d3d574

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from fpdf import FPDF
3
+ from groq import Groq
4
+ import os
5
+
6
+ # Set the Groq API key from Hugging Face Secrets (ensure to add it in your Space's settings)
7
+ api_key = os.getenv("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
8
+ ")
9
+ client = Groq(api_key=api_key)
10
+
11
+ # Function to generate a timetable
12
+ def generate_timetable(teachers, subjects, slots):
13
+ """
14
+ Generate a timetable using the Groq API.
15
+ """
16
+ try:
17
+ prompt = f"""
18
+ Create a weekly timetable given the following data:
19
+ Teachers: {", ".join(teachers)}
20
+ Subjects: {", ".join(subjects)}
21
+ Available Slots: {", ".join(slots)}.
22
+ """
23
+
24
+ chat_completion = client.chat.completions.create(
25
+ messages=[{"role": "user", "content": prompt}],
26
+ model="llama3-8b-8192",
27
+ )
28
+
29
+ return chat_completion.choices[0].message.content
30
+
31
+ except Exception as e:
32
+ st.error(f"Error generating timetable: {e}")
33
+ return None
34
+
35
+ # Function to save timetable as PDF
36
+ def save_timetable_as_pdf(timetable, filename="timetable.pdf"):
37
+ """
38
+ Save the generated timetable as a PDF.
39
+ """
40
+ pdf = FPDF()
41
+ pdf.add_page()
42
+ pdf.set_font("Arial", size=12)
43
+
44
+ pdf.cell(200, 10, txt="Class Timetable", ln=True, align="C")
45
+ pdf.ln(10) # Add some space
46
+
47
+ for line in timetable.split("\n"):
48
+ pdf.cell(200, 10, txt=line, ln=True)
49
+
50
+ pdf.output(filename)
51
+ return filename
52
+
53
+ # Streamlit App UI
54
+ st.title("Class Timetable Generator")
55
+ st.write("Generate and manage class schedules effortlessly.")
56
+
57
+ # Input fields
58
+ teachers = st.text_input("Enter the names of teachers (comma-separated)").split(",")
59
+ subjects = st.text_input("Enter the subjects (comma-separated)").split(",")
60
+ slots = st.text_input("Enter the available slots (comma-separated)").split(",")
61
+
62
+ if st.button("Generate Timetable"):
63
+ if teachers and subjects and slots:
64
+ st.info("Generating timetable...")
65
+ timetable = generate_timetable(teachers, subjects, slots)
66
+
67
+ if timetable:
68
+ st.success("Timetable generated successfully!")
69
+ st.text_area("Generated Timetable", timetable, height=300)
70
+
71
+ # Option to download the timetable as a PDF
72
+ if st.button("Download PDF"):
73
+ pdf_file = save_timetable_as_pdf(timetable)
74
+ with open(pdf_file, "rb") as file:
75
+ st.download_button("Download Timetable PDF", file, file_name="timetable.pdf")
76
+ else:
77
+ st.error("Failed to generate the timetable. Please check the inputs or API settings.")
78
+ else:
79
+ st.warning("Please fill all fields to generate the timetable.")