MiakOnline commited on
Commit
6cd9985
Β·
verified Β·
1 Parent(s): ca8efdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -5
app.py CHANGED
@@ -15,7 +15,29 @@ import shutil
15
 
16
  # ----------------------------- UI SETUP --------------------------------------
17
  st.set_page_config(page_title="Learning with Fun", layout="wide")
18
- st.title("πŸ“š Learning with Fun - Educational Q&A for Kids")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # ----------------------------- USER INPUT -----------------------------------
21
  grade = st.selectbox("Select your Grade", ["Grade 5", "Grade 6"])
@@ -23,7 +45,11 @@ subject = st.selectbox("Select Subject", ["Science", "Math", "English"])
23
 
24
  uploaded_files = st.file_uploader("Upload textbook files (PDF, DOCX, JPEG)", type=["pdf", "docx", "jpg", "jpeg"], accept_multiple_files=True)
25
  question = st.text_input("Ask your question in English or Urdu")
26
- groq_api_key = st.text_input("πŸ” Enter your GROQ API Key", type="password")
 
 
 
 
27
 
28
  # ------------------------- SETUP TEMP FOLDER -------------------------------
29
  temp_dir = tempfile.mkdtemp()
@@ -105,12 +131,12 @@ def generate_audio(text, lang='ur'):
105
  with open(audio_path, "rb") as audio_file:
106
  audio_bytes = audio_file.read()
107
  b64 = base64.b64encode(audio_bytes).decode()
108
- audio_html = f'<audio autoplay controls><source src="data:audio/mp3;base64,{b64}" type="audio/mp3"></audio>'
109
  return audio_html
110
 
111
  # ----------------------------- MAIN LOGIC ----------------------------------
112
  if question and uploaded_files and groq_api_key:
113
- with st.spinner("Processing your documents..."):
114
  documents = load_documents(uploaded_files)
115
  chunks = split_documents(documents)
116
  vector_db = create_vector_store(chunks)
@@ -124,7 +150,9 @@ if question and uploaded_files and groq_api_key:
124
  if len(parts) == 2:
125
  st.markdown(f"**Explanation:**\n{parts[0]}")
126
  st.markdown(f"**Storytelling:**\n{parts[1]}")
127
- st.markdown(generate_audio(parts[1]), unsafe_allow_html=True)
 
 
128
  else:
129
  st.markdown(answer)
130
 
 
15
 
16
  # ----------------------------- UI SETUP --------------------------------------
17
  st.set_page_config(page_title="Learning with Fun", layout="wide")
18
+ st.markdown("""
19
+ <style>
20
+ .main {
21
+ background-color: #f0f8ff;
22
+ }
23
+ .block-container {
24
+ padding-top: 2rem;
25
+ }
26
+ .stSelectbox > label, .stTextInput > label {
27
+ font-size: 18px;
28
+ font-weight: bold;
29
+ color: #2e7d32;
30
+ }
31
+ .stTextInput input {
32
+ font-size: 16px;
33
+ padding: 10px;
34
+ border-radius: 10px;
35
+ }
36
+ </style>
37
+ """, unsafe_allow_html=True)
38
+
39
+ st.title("🌈 Learning with Fun πŸŽ“")
40
+ st.subheader("Helping Kids Learn Through Interactive Books, Questions & Stories!")
41
 
42
  # ----------------------------- USER INPUT -----------------------------------
43
  grade = st.selectbox("Select your Grade", ["Grade 5", "Grade 6"])
 
45
 
46
  uploaded_files = st.file_uploader("Upload textbook files (PDF, DOCX, JPEG)", type=["pdf", "docx", "jpg", "jpeg"], accept_multiple_files=True)
47
  question = st.text_input("Ask your question in English or Urdu")
48
+
49
+ # ----------------------------- ENV VAR SETUP -----------------------------------
50
+ groq_api_key = os.getenv("GROQ_API_KEY", "")
51
+ if not groq_api_key:
52
+ st.warning("GROQ API key is not set in the environment. Please configure it as a Hugging Face Secret with the name 'GROQ_API_KEY'.")
53
 
54
  # ------------------------- SETUP TEMP FOLDER -------------------------------
55
  temp_dir = tempfile.mkdtemp()
 
131
  with open(audio_path, "rb") as audio_file:
132
  audio_bytes = audio_file.read()
133
  b64 = base64.b64encode(audio_bytes).decode()
134
+ audio_html = f'<audio controls><source src="data:audio/mp3;base64,{b64}" type="audio/mp3"></audio>'
135
  return audio_html
136
 
137
  # ----------------------------- MAIN LOGIC ----------------------------------
138
  if question and uploaded_files and groq_api_key:
139
+ with st.spinner("Processing your documents and generating answer..."):
140
  documents = load_documents(uploaded_files)
141
  chunks = split_documents(documents)
142
  vector_db = create_vector_store(chunks)
 
150
  if len(parts) == 2:
151
  st.markdown(f"**Explanation:**\n{parts[0]}")
152
  st.markdown(f"**Storytelling:**\n{parts[1]}")
153
+
154
+ if st.button("πŸ”Š Play Storytelling Voice"):
155
+ st.markdown(generate_audio(parts[1]), unsafe_allow_html=True)
156
  else:
157
  st.markdown(answer)
158