sunbal7 commited on
Commit
cde1a59
Β·
verified Β·
1 Parent(s): df4bf8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
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
- # βœ… Streamlit Page Config
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
- # βœ… Extract text from files
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
- 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="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 Generation
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
- prompt = f"""Create a detailed {days}-day study plan from the syllabus below.
61
- You should assume the user can study {hours_per_day} hours per day.
62
- Output should be in {language}.
 
 
 
 
63
 
64
  Syllabus:
65
  \"\"\"
66
- {content}
67
  \"\"\"
68
  """
69
- return query_groq(prompt)
 
 
 
 
70
  except Exception as e:
71
  return f"⚠️ Error: {str(e)}"
72
 
73
- # βœ… Question Answering
74
  def ask_question(file, question):
75
  try:
76
  context = extract_text(file)
77
- prompt = f"""Based on the following study material, answer the question below:
 
 
 
 
78
 
79
- Study Material:
80
  \"\"\"
81
- {context}
82
  \"\"\"
83
 
84
  Question: {question}
85
  Answer:"""
86
- return query_groq(prompt)
 
 
 
 
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=200)
 
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)