Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,21 @@
|
|
1 |
import os
|
2 |
-
import tempfile
|
3 |
from datetime import datetime
|
4 |
from PyPDF2 import PdfReader
|
5 |
from docx import Document
|
6 |
import streamlit as st
|
7 |
-
from
|
8 |
|
9 |
-
# β
Page
|
10 |
st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
|
11 |
|
12 |
-
# Load
|
13 |
@st.cache_resource
|
14 |
-
def
|
15 |
-
|
16 |
-
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
17 |
-
return summarizer, qa_pipeline
|
18 |
|
19 |
-
|
20 |
|
21 |
-
# Extract text from
|
22 |
def extract_text(file):
|
23 |
ext = os.path.splitext(file.name)[1].lower()
|
24 |
if ext == ".txt":
|
@@ -30,12 +27,25 @@ def extract_text(file):
|
|
30 |
pdf_reader = PdfReader(file)
|
31 |
text = ""
|
32 |
for page in pdf_reader.pages:
|
33 |
-
|
|
|
34 |
return text
|
35 |
else:
|
36 |
raise ValueError("Only .txt, .docx, and .pdf files are supported.")
|
37 |
|
38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def generate_plan(file, hours_per_day, exam_date, language):
|
40 |
try:
|
41 |
content = extract_text(file)
|
@@ -46,26 +56,37 @@ def generate_plan(file, hours_per_day, exam_date, language):
|
|
46 |
if days <= 0:
|
47 |
return "β Exam date must be in the future."
|
48 |
|
49 |
-
prompt = f"Create a {days}-day study plan
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
summary = summarizer(prompt, max_length=512, min_length=150, do_sample=False)
|
54 |
-
return summary[0]['summary_text']
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
except Exception as e:
|
57 |
return f"β οΈ Error: {str(e)}"
|
58 |
|
59 |
-
#
|
60 |
def ask_question(file, question):
|
61 |
try:
|
62 |
context = extract_text(file)
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
except Exception as e:
|
66 |
return f"β οΈ Error: {str(e)}"
|
67 |
|
68 |
-
# Streamlit UI
|
69 |
st.sidebar.title("π Study Assistant Options")
|
70 |
uploaded_file = st.sidebar.file_uploader("Upload syllabus (.txt, .docx, .pdf)", type=["txt", "docx", "pdf"])
|
71 |
study_hours = st.sidebar.number_input("Study hours per day", min_value=1, max_value=12, value=3)
|
|
|
1 |
import os
|
|
|
2 |
from datetime import datetime
|
3 |
from PyPDF2 import PdfReader
|
4 |
from docx import Document
|
5 |
import streamlit as st
|
6 |
+
from groq import Groq
|
7 |
|
8 |
+
# β
Streamlit Page Config
|
9 |
st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
|
10 |
|
11 |
+
# β
Load Groq Client
|
12 |
@st.cache_resource
|
13 |
+
def load_groq_client():
|
14 |
+
return Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
|
|
|
15 |
|
16 |
+
groq_client = load_groq_client()
|
17 |
|
18 |
+
# β
Extract text from files
|
19 |
def extract_text(file):
|
20 |
ext = os.path.splitext(file.name)[1].lower()
|
21 |
if ext == ".txt":
|
|
|
27 |
pdf_reader = PdfReader(file)
|
28 |
text = ""
|
29 |
for page in pdf_reader.pages:
|
30 |
+
if page.extract_text():
|
31 |
+
text += page.extract_text() + "\n"
|
32 |
return text
|
33 |
else:
|
34 |
raise ValueError("Only .txt, .docx, and .pdf files are supported.")
|
35 |
|
36 |
+
# β
Query Groq LLM
|
37 |
+
def query_groq(prompt, temperature=0.5):
|
38 |
+
try:
|
39 |
+
chat = groq_client.chat.completions.create(
|
40 |
+
messages=[{"role": "user", "content": prompt}],
|
41 |
+
model="mixtral-8x7b-32768",
|
42 |
+
temperature=temperature
|
43 |
+
)
|
44 |
+
return chat.choices[0].message.content.strip()
|
45 |
+
except Exception as e:
|
46 |
+
return f"β οΈ Groq API Error: {str(e)}"
|
47 |
+
|
48 |
+
# β
Study Plan Generation
|
49 |
def generate_plan(file, hours_per_day, exam_date, language):
|
50 |
try:
|
51 |
content = extract_text(file)
|
|
|
56 |
if days <= 0:
|
57 |
return "β Exam date must be in the future."
|
58 |
|
59 |
+
prompt = f"""Create a detailed {days}-day study plan from the syllabus below.
|
60 |
+
You should assume the user can study {hours_per_day} hours per day.
|
61 |
+
Output should be in {language}.
|
|
|
|
|
|
|
62 |
|
63 |
+
Syllabus:
|
64 |
+
\"\"\"
|
65 |
+
{content}
|
66 |
+
\"\"\"
|
67 |
+
"""
|
68 |
+
return query_groq(prompt)
|
69 |
except Exception as e:
|
70 |
return f"β οΈ Error: {str(e)}"
|
71 |
|
72 |
+
# β
Question Answering
|
73 |
def ask_question(file, question):
|
74 |
try:
|
75 |
context = extract_text(file)
|
76 |
+
prompt = f"""Based on the following study material, answer the question below:
|
77 |
+
|
78 |
+
Study Material:
|
79 |
+
\"\"\"
|
80 |
+
{context}
|
81 |
+
\"\"\"
|
82 |
+
|
83 |
+
Question: {question}
|
84 |
+
Answer:"""
|
85 |
+
return query_groq(prompt)
|
86 |
except Exception as e:
|
87 |
return f"β οΈ Error: {str(e)}"
|
88 |
|
89 |
+
# β
Streamlit UI
|
90 |
st.sidebar.title("π Study Assistant Options")
|
91 |
uploaded_file = st.sidebar.file_uploader("Upload syllabus (.txt, .docx, .pdf)", type=["txt", "docx", "pdf"])
|
92 |
study_hours = st.sidebar.number_input("Study hours per day", min_value=1, max_value=12, value=3)
|