Hifzamudassar commited on
Commit
82bb22c
·
verified ·
1 Parent(s): 78236ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -24
app.py CHANGED
@@ -118,17 +118,21 @@ def generate_notes_flowchart(text):
118
  return notes,img
119
 
120
  # =====================
121
- # FILE OCR
122
  # =====================
123
  def extract_text_from_file(file):
124
- if file.name.endswith(".pdf"):
125
- pages=convert_from_path(file.name)
126
- text=""
127
- for page in pages:
128
- text+=pytesseract.image_to_string(page)+"\n"
129
- return text
130
- else:
131
- return pytesseract.image_to_string(Image.open(file.name))
 
 
 
 
132
 
133
  # =====================
134
  # LINKS
@@ -147,11 +151,64 @@ custom_css="""
147
  body {background: linear-gradient(to bottom right, #90ee90, #ffb6c1);}
148
  """
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  # =====================
151
  # GRADIO UI
152
  # =====================
153
  with gr.Blocks() as demo:
154
- # Inject CSS for older Gradio versions
155
  gr.HTML(f"<style>{custom_css}</style>")
156
  gr.Markdown("## 🌸 AI Study Wizard — Pink + Green Gradient")
157
 
@@ -208,20 +265,6 @@ with gr.Blocks() as demo:
208
  fv2=gr.Video(label="Full Video",width=640,height=360)
209
  notes_out=gr.Textbox(label="Notes",lines=12)
210
  flow_out=gr.Image(label="Flowchart",type="pil")
211
- def process_file(file,options,lg):
212
- text=extract_text_from_file(file)
213
- mcq=""
214
- prev=None
215
- full=None
216
- notes=""
217
- flow=None
218
- if "MCQs" in options: mcq=generate_mcqs_from_text(text)
219
- if "Video" in options:
220
- prev=create_video_simple(text,lg,preview=True)
221
- full=create_video_simple(text,lg)
222
- if "Notes" in options:
223
- notes,flow=generate_notes_flowchart(text)
224
- return text,mcq,prev,full,notes,flow
225
  btn=gr.Button("Process")
226
  btn.click(process_file,[f_in,opts,lang],[t_out,mc_out,pv2,fv2,notes_out,flow_out])
227
 
 
118
  return notes,img
119
 
120
  # =====================
121
+ # FILE OCR (Safe)
122
  # =====================
123
  def extract_text_from_file(file):
124
+ try:
125
+ if file.name.endswith(".pdf"):
126
+ pages=convert_from_path(file.name)
127
+ text=""
128
+ for page in pages:
129
+ text+=pytesseract.image_to_string(page)+"\n"
130
+ return text
131
+ else:
132
+ img = Image.open(file.name)
133
+ return pytesseract.image_to_string(img)
134
+ except Exception as e:
135
+ return f"Error reading file: {e}"
136
 
137
  # =====================
138
  # LINKS
 
151
  body {background: linear-gradient(to bottom right, #90ee90, #ffb6c1);}
152
  """
153
 
154
+ # =====================
155
+ # UPLOAD PROCESS (Fixed)
156
+ # =====================
157
+ def process_file(file, options, lg):
158
+ text=""
159
+ mcq=""
160
+ prev=None
161
+ full=None
162
+ notes=""
163
+ flow=None
164
+
165
+ if file is None:
166
+ return "No file uploaded.","",None,None,"",None
167
+
168
+ # Extract text safely
169
+ try:
170
+ if file.name.endswith(".pdf"):
171
+ pages=convert_from_path(file.name)
172
+ for page in pages:
173
+ text+=pytesseract.image_to_string(page)+"\n"
174
+ else:
175
+ img = Image.open(file.name)
176
+ text = pytesseract.image_to_string(img)
177
+ except Exception as e:
178
+ text=f"Error extracting text: {e}"
179
+
180
+ # Generate MCQs
181
+ if "MCQs" in options and text.strip():
182
+ try:
183
+ mcq = generate_mcqs_from_text(text)
184
+ except Exception as e:
185
+ mcq=f"Error generating MCQs: {e}"
186
+
187
+ # Generate Video
188
+ if "Video" in options and text.strip():
189
+ try:
190
+ prev=create_video_simple(text,lg,preview=True)
191
+ full=create_video_simple(text,lg)
192
+ except Exception as e:
193
+ prev=None
194
+ full=None
195
+ text += f"\n[Video error: {e}]"
196
+
197
+ # Generate Notes & Flowchart
198
+ if "Notes" in options and text.strip():
199
+ try:
200
+ notes,flow = generate_notes_flowchart(text)
201
+ except Exception as e:
202
+ notes=""
203
+ flow=None
204
+ text += f"\n[Notes/Flowchart error: {e}]"
205
+
206
+ return text, mcq, prev, full, notes, flow
207
+
208
  # =====================
209
  # GRADIO UI
210
  # =====================
211
  with gr.Blocks() as demo:
 
212
  gr.HTML(f"<style>{custom_css}</style>")
213
  gr.Markdown("## 🌸 AI Study Wizard — Pink + Green Gradient")
214
 
 
265
  fv2=gr.Video(label="Full Video",width=640,height=360)
266
  notes_out=gr.Textbox(label="Notes",lines=12)
267
  flow_out=gr.Image(label="Flowchart",type="pil")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  btn=gr.Button("Process")
269
  btn.click(process_file,[f_in,opts,lang],[t_out,mc_out,pv2,fv2,notes_out,flow_out])
270