MiakOnline commited on
Commit
3cc40cc
·
verified ·
1 Parent(s): 4507d2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -16
app.py CHANGED
@@ -10,29 +10,27 @@ from langchain.embeddings import HuggingFaceEmbeddings
10
  from langchain.text_splitter import CharacterTextSplitter
11
  from langchain_core.prompts import PromptTemplate
12
  from transformers import pipeline
13
- from langchain.chains import RetrievalQA
14
  from langchain.llms import HuggingFacePipeline
15
 
16
- # Set up HuggingFace text-generation pipeline (you can change the model)
17
  text_gen_pipeline = pipeline(
18
  "text-generation",
19
- model="gpt2", # Small model for demo; replace with your preferred model
20
  max_length=150
21
  )
22
  llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
23
 
24
- # App UI
25
  st.set_page_config(page_title="Learning with Fun", layout="wide")
26
  st.title("📘 Learning with Fun - Kids QA App")
27
  st.markdown("Ask questions from your syllabus! 📚")
28
 
29
- # Sidebar controls
30
  grade = st.sidebar.selectbox("Select Grade", ["Grade 5", "Grade 6"])
31
  subject = st.sidebar.selectbox("Select Subject", ["Science", "Math", "Computer", "Islamiyat"])
32
  mode = st.sidebar.radio("Answer Format", ["🧠 Beginner Explanation", "📖 Storytelling"])
33
  voice_enabled = st.sidebar.checkbox("🔈 Enable Voice", value=True)
34
 
35
- # Fetch file from Google Drive
36
  def fetch_from_gdrive(link):
37
  file_id = None
38
  if "id=" in link:
@@ -60,7 +58,6 @@ if file_link:
60
  else:
61
  st.error("Invalid Google Drive link or download error.")
62
 
63
- # Extract text from PDF or DOCX
64
  def extract_text(file_path):
65
  text = ""
66
  if file_path.endswith(".pdf"):
@@ -75,7 +72,6 @@ def extract_text(file_path):
75
  text += para.text + "\n"
76
  return text
77
 
78
- # Create vectorstore for retrieval
79
  def create_vectorstore(text):
80
  splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
81
  docs = splitter.create_documents([text])
@@ -83,37 +79,32 @@ def create_vectorstore(text):
83
  vectorstore = FAISS.from_documents(docs, embeddings)
84
  return vectorstore
85
 
86
- # Prompts
87
  story_prompt = PromptTemplate.from_template(
88
  "ایک طالب علم نے سوال کیا: {question}\n"
89
  "نصاب کی معلومات: {context}\n"
90
  "برائے مہربانی ایک دلچسپ کہانی کی صورت میں بچے کو اردو میں جواب دیں۔"
91
  )
 
92
  explain_prompt = PromptTemplate.from_template(
93
  "سوال: {question}\n"
94
  "نصاب کا سیاق و سباق: {context}\n"
95
  "براہ کرم بچے کو اردو زبان میں آسان انداز میں سمجھائیں۔"
96
  )
97
 
98
- # Generate voice from text
99
  def generate_voice(text, lang='ur'):
100
  tts = gTTS(text, lang=lang)
101
  tts_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
102
  tts.save(tts_file.name)
103
  return tts_file.name
104
 
105
- # Get answer using LLM
106
  def get_answer(query, vectorstore, mode):
107
  retriever = vectorstore.as_retriever()
108
  docs = retriever.get_relevant_documents(query)
109
  context = "\n".join([doc.page_content for doc in docs])
110
  prompt = story_prompt.format(question=query, context=context) if mode == "📖 Storytelling" else explain_prompt.format(question=query, context=context)
111
- # Use LLM pipeline to generate answer text
112
- response = llm.invoke(prompt)
113
- # llm.invoke returns a string answer
114
- return response
115
 
116
- # Main app flow
117
  if uploaded_file:
118
  raw_text = extract_text(uploaded_file)
119
  st.success("📄 Syllabus loaded successfully!")
 
10
  from langchain.text_splitter import CharacterTextSplitter
11
  from langchain_core.prompts import PromptTemplate
12
  from transformers import pipeline
 
13
  from langchain.llms import HuggingFacePipeline
14
 
15
+ # Setup HuggingFace text generation pipeline (replace model with your choice)
16
  text_gen_pipeline = pipeline(
17
  "text-generation",
18
+ model="gpt2", # lightweight model for demo, swap for bigger model as needed
19
  max_length=150
20
  )
21
  llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
22
 
23
+ # Streamlit UI setup
24
  st.set_page_config(page_title="Learning with Fun", layout="wide")
25
  st.title("📘 Learning with Fun - Kids QA App")
26
  st.markdown("Ask questions from your syllabus! 📚")
27
 
28
+ # Sidebar options
29
  grade = st.sidebar.selectbox("Select Grade", ["Grade 5", "Grade 6"])
30
  subject = st.sidebar.selectbox("Select Subject", ["Science", "Math", "Computer", "Islamiyat"])
31
  mode = st.sidebar.radio("Answer Format", ["🧠 Beginner Explanation", "📖 Storytelling"])
32
  voice_enabled = st.sidebar.checkbox("🔈 Enable Voice", value=True)
33
 
 
34
  def fetch_from_gdrive(link):
35
  file_id = None
36
  if "id=" in link:
 
58
  else:
59
  st.error("Invalid Google Drive link or download error.")
60
 
 
61
  def extract_text(file_path):
62
  text = ""
63
  if file_path.endswith(".pdf"):
 
72
  text += para.text + "\n"
73
  return text
74
 
 
75
  def create_vectorstore(text):
76
  splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
77
  docs = splitter.create_documents([text])
 
79
  vectorstore = FAISS.from_documents(docs, embeddings)
80
  return vectorstore
81
 
 
82
  story_prompt = PromptTemplate.from_template(
83
  "ایک طالب علم نے سوال کیا: {question}\n"
84
  "نصاب کی معلومات: {context}\n"
85
  "برائے مہربانی ایک دلچسپ کہانی کی صورت میں بچے کو اردو میں جواب دیں۔"
86
  )
87
+
88
  explain_prompt = PromptTemplate.from_template(
89
  "سوال: {question}\n"
90
  "نصاب کا سیاق و سباق: {context}\n"
91
  "براہ کرم بچے کو اردو زبان میں آسان انداز میں سمجھائیں۔"
92
  )
93
 
 
94
  def generate_voice(text, lang='ur'):
95
  tts = gTTS(text, lang=lang)
96
  tts_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
97
  tts.save(tts_file.name)
98
  return tts_file.name
99
 
 
100
  def get_answer(query, vectorstore, mode):
101
  retriever = vectorstore.as_retriever()
102
  docs = retriever.get_relevant_documents(query)
103
  context = "\n".join([doc.page_content for doc in docs])
104
  prompt = story_prompt.format(question=query, context=context) if mode == "📖 Storytelling" else explain_prompt.format(question=query, context=context)
105
+ answer = llm.invoke(prompt)
106
+ return answer
 
 
107
 
 
108
  if uploaded_file:
109
  raw_text = extract_text(uploaded_file)
110
  st.success("📄 Syllabus loaded successfully!")