sunbal7 commited on
Commit
a9d0ada
Β·
verified Β·
1 Parent(s): 242da2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -22
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 transformers import pipeline
8
 
9
- # βœ… Page config MUST be first Streamlit command
10
  st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
11
 
12
- # Load models with caching
13
  @st.cache_resource
14
- def load_models():
15
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
16
- qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
17
- return summarizer, qa_pipeline
18
 
19
- summarizer, qa_pipeline = load_models()
20
 
21
- # Extract text from supported formats
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
- text += page.extract_text() + "\n"
 
34
  return text
35
  else:
36
  raise ValueError("Only .txt, .docx, and .pdf files are supported.")
37
 
38
- # Generate study plan
 
 
 
 
 
 
 
 
 
 
 
 
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 for this syllabus:\n{content}\nDaily study time: {hours_per_day} hours."
50
- if language == "Urdu":
51
- prompt += "\nTranslate the plan into Urdu."
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
- # Ask a question
60
  def ask_question(file, question):
61
  try:
62
  context = extract_text(file)
63
- result = qa_pipeline({"context": context, "question": question})
64
- return result['answer']
 
 
 
 
 
 
 
 
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)