Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,9 @@ from PyPDF2 import PdfReader
|
|
4 |
from docx import Document
|
5 |
import streamlit as st
|
6 |
from groq import Groq
|
|
|
7 |
|
8 |
-
# β
|
9 |
st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
|
10 |
|
11 |
# β
Load Groq Client
|
@@ -15,7 +16,7 @@ def load_groq_client():
|
|
15 |
|
16 |
groq_client = load_groq_client()
|
17 |
|
18 |
-
# β
|
19 |
def extract_text(file):
|
20 |
ext = os.path.splitext(file.name)[1].lower()
|
21 |
if ext == ".txt":
|
@@ -27,26 +28,30 @@ def extract_text(file):
|
|
27 |
pdf_reader = PdfReader(file)
|
28 |
text = ""
|
29 |
for page in pdf_reader.pages:
|
30 |
-
|
31 |
-
|
|
|
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.
|
38 |
try:
|
39 |
chat = groq_client.chat.completions.create(
|
40 |
messages=[{"role": "user", "content": prompt}],
|
41 |
model="llama3-8b-8192",
|
42 |
temperature=temperature
|
43 |
-
)
|
44 |
-
|
45 |
return chat.choices[0].message.content.strip()
|
46 |
except Exception as e:
|
47 |
return f"β οΈ Groq API Error: {str(e)}"
|
48 |
|
49 |
-
# β
Study Plan
|
50 |
def generate_plan(file, hours_per_day, exam_date, language):
|
51 |
try:
|
52 |
content = extract_text(file)
|
@@ -57,33 +62,49 @@ def generate_plan(file, hours_per_day, exam_date, language):
|
|
57 |
if days <= 0:
|
58 |
return "β Exam date must be in the future."
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
|
64 |
Syllabus:
|
65 |
\"\"\"
|
66 |
-
{
|
67 |
\"\"\"
|
68 |
"""
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
71 |
return f"β οΈ Error: {str(e)}"
|
72 |
|
73 |
-
# β
Question
|
74 |
def ask_question(file, question):
|
75 |
try:
|
76 |
context = extract_text(file)
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
\"\"\"
|
81 |
-
{
|
82 |
\"\"\"
|
83 |
|
84 |
Question: {question}
|
85 |
Answer:"""
|
86 |
-
|
|
|
|
|
|
|
|
|
87 |
except Exception as e:
|
88 |
return f"β οΈ Error: {str(e)}"
|
89 |
|
@@ -109,4 +130,4 @@ with tab2:
|
|
109 |
question = st.text_input("Enter your question:")
|
110 |
if uploaded_file and question and st.button("Get Answer"):
|
111 |
answer = ask_question(uploaded_file, question)
|
112 |
-
st.text_area("Answer", answer, height=
|
|
|
4 |
from docx import Document
|
5 |
import streamlit as st
|
6 |
from groq import Groq
|
7 |
+
import textwrap
|
8 |
|
9 |
+
# β
Page Configuration
|
10 |
st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
|
11 |
|
12 |
# β
Load Groq Client
|
|
|
16 |
|
17 |
groq_client = load_groq_client()
|
18 |
|
19 |
+
# β
File Text Extraction
|
20 |
def extract_text(file):
|
21 |
ext = os.path.splitext(file.name)[1].lower()
|
22 |
if ext == ".txt":
|
|
|
28 |
pdf_reader = PdfReader(file)
|
29 |
text = ""
|
30 |
for page in pdf_reader.pages:
|
31 |
+
page_text = page.extract_text()
|
32 |
+
if page_text:
|
33 |
+
text += page_text + "\n"
|
34 |
return text
|
35 |
else:
|
36 |
raise ValueError("Only .txt, .docx, and .pdf files are supported.")
|
37 |
|
38 |
+
# β
Chunking helper
|
39 |
+
def chunk_text(text, chunk_size=1500):
|
40 |
+
return textwrap.wrap(text, width=chunk_size, break_long_words=False, replace_whitespace=False)
|
41 |
+
|
42 |
# β
Query Groq LLM
|
43 |
+
def query_groq(prompt, temperature=0.4):
|
44 |
try:
|
45 |
chat = groq_client.chat.completions.create(
|
46 |
messages=[{"role": "user", "content": prompt}],
|
47 |
model="llama3-8b-8192",
|
48 |
temperature=temperature
|
49 |
+
)
|
|
|
50 |
return chat.choices[0].message.content.strip()
|
51 |
except Exception as e:
|
52 |
return f"β οΈ Groq API Error: {str(e)}"
|
53 |
|
54 |
+
# β
Generate Study Plan with chunking
|
55 |
def generate_plan(file, hours_per_day, exam_date, language):
|
56 |
try:
|
57 |
content = extract_text(file)
|
|
|
62 |
if days <= 0:
|
63 |
return "β Exam date must be in the future."
|
64 |
|
65 |
+
chunks = chunk_text(content, chunk_size=1500)
|
66 |
+
plan_parts = []
|
67 |
+
|
68 |
+
for idx, chunk in enumerate(chunks):
|
69 |
+
prompt = f"""This is part {idx + 1} of {len(chunks)} of a syllabus.
|
70 |
+
Create a study plan segment for this syllabus. Total study duration is {days} days with {hours_per_day} hours/day.
|
71 |
+
Write the study plan in {language}.
|
72 |
|
73 |
Syllabus:
|
74 |
\"\"\"
|
75 |
+
{chunk}
|
76 |
\"\"\"
|
77 |
"""
|
78 |
+
response = query_groq(prompt)
|
79 |
+
plan_parts.append(response)
|
80 |
+
|
81 |
+
return "\n\n".join(plan_parts)
|
82 |
+
|
83 |
except Exception as e:
|
84 |
return f"β οΈ Error: {str(e)}"
|
85 |
|
86 |
+
# β
Ask Question with context chunking
|
87 |
def ask_question(file, question):
|
88 |
try:
|
89 |
context = extract_text(file)
|
90 |
+
chunks = chunk_text(context, chunk_size=1500)
|
91 |
+
answers = []
|
92 |
+
|
93 |
+
for idx, chunk in enumerate(chunks):
|
94 |
+
prompt = f"""Use the following part of study material to answer the question:
|
95 |
|
96 |
+
Material:
|
97 |
\"\"\"
|
98 |
+
{chunk}
|
99 |
\"\"\"
|
100 |
|
101 |
Question: {question}
|
102 |
Answer:"""
|
103 |
+
answer = query_groq(prompt)
|
104 |
+
answers.append(f"Part {idx + 1}:\n{answer}")
|
105 |
+
|
106 |
+
return "\n\n".join(answers)
|
107 |
+
|
108 |
except Exception as e:
|
109 |
return f"β οΈ Error: {str(e)}"
|
110 |
|
|
|
130 |
question = st.text_input("Enter your question:")
|
131 |
if uploaded_file and question and st.button("Get Answer"):
|
132 |
answer = ask_question(uploaded_file, question)
|
133 |
+
st.text_area("Answer", answer, height=300)
|