167AliRaza commited on
Commit
bce1ee4
·
verified ·
1 Parent(s): 64555d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -3,9 +3,9 @@ import pandas as pd
3
  from pdf2image import convert_from_path
4
  import pytesseract
5
  import google.generativeai as genai
6
- from io import BytesIO
7
 
8
- # Function: Extract text from PDF
9
  def extract_text_from_pdf(pdf_file):
10
  pages = convert_from_path(pdf_file)
11
  all_text = ""
@@ -70,7 +70,7 @@ def generate_mcqs(text, api_key):
70
  return None, None
71
 
72
  df = pd.DataFrame(filtered_mcq_data, columns=["Question", "OptionA", "OptionB", "OptionC", "OptionD", "CorrectAnswer"])
73
- return df, df.head(10).to_markdown() # Show preview
74
 
75
  # Gradio pipeline
76
  def process_pdf(pdf_file, api_key):
@@ -84,12 +84,11 @@ def process_pdf(pdf_file, api_key):
84
  if df is None:
85
  return "❌ No valid MCQs generated.", None
86
 
87
- # Save to Excel in memory
88
- output_file = BytesIO()
89
- df.to_excel(output_file, index=False)
90
- output_file.seek(0)
91
 
92
- return preview, output_file
93
  except Exception as e:
94
  return f"Error: {str(e)}", None
95
 
 
3
  from pdf2image import convert_from_path
4
  import pytesseract
5
  import google.generativeai as genai
6
+ import tempfile
7
 
8
+ # Function: Extract text from PDF using OCR
9
  def extract_text_from_pdf(pdf_file):
10
  pages = convert_from_path(pdf_file)
11
  all_text = ""
 
70
  return None, None
71
 
72
  df = pd.DataFrame(filtered_mcq_data, columns=["Question", "OptionA", "OptionB", "OptionC", "OptionD", "CorrectAnswer"])
73
+ return df, df.head(10).to_string(index=False) # Preview as plain table
74
 
75
  # Gradio pipeline
76
  def process_pdf(pdf_file, api_key):
 
84
  if df is None:
85
  return "❌ No valid MCQs generated.", None
86
 
87
+ # Save to a temporary Excel file
88
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
89
+ df.to_excel(tmp_file.name, index=False)
 
90
 
91
+ return preview, tmp_file.name
92
  except Exception as e:
93
  return f"Error: {str(e)}", None
94