Sayandip commited on
Commit
6cf4d5c
·
verified ·
1 Parent(s): 4f2d9ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -24
app.py CHANGED
@@ -9,7 +9,6 @@ from pptx import Presentation
9
  from io import StringIO
10
  from bs4 import BeautifulSoup
11
  import re
12
- import json
13
  import google.generativeai as genai
14
 
15
  # Setup
@@ -73,22 +72,6 @@ def load_and_extract_text(file_path):
73
  script_or_style.decompose()
74
  return soup.get_text(separator='\n', strip=True)
75
 
76
- elif ext == 'py':
77
- with open(file_path, 'r', encoding='utf-8') as f:
78
- return f.read() # We just read the Python code content
79
-
80
- elif ext == 'ipynb':
81
- with open(file_path, 'r', encoding='utf-8') as f:
82
- notebook = json.load(f)
83
- cells = notebook.get("cells", [])
84
- extracted = []
85
- for cell in cells:
86
- if cell.get("cell_type") == "markdown":
87
- extracted.append("### Markdown:\n" + ''.join(cell.get("source", [])))
88
- elif cell.get("cell_type") == "code":
89
- extracted.append("### Code:\n" + ''.join(cell.get("source", [])))
90
- return '\n'.join(extracted)
91
-
92
  else:
93
  return "Unsupported file format."
94
 
@@ -98,10 +81,7 @@ def load_and_extract_text(file_path):
98
  # File uploader
99
  uploaded_file = st.file_uploader(
100
  "Upload a document or image",
101
- type=[
102
- "docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm",
103
- "jpg", "jpeg", "png", "py", "ipynb"
104
- ]
105
  )
106
 
107
  # Question input
@@ -146,11 +126,9 @@ if uploaded_file and question:
146
  else:
147
  with st.spinner("Processing document..."):
148
  try:
149
- # Hide the extracted content
150
- st.write("Processing your query based on the uploaded content...")
151
  prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
152
  response = model.generate_content(prompt)
153
  st.success("💡 Answer:")
154
  st.write(response.text)
155
  except Exception as e:
156
- st.error(f"Error generating answer: {e}")
 
9
  from io import StringIO
10
  from bs4 import BeautifulSoup
11
  import re
 
12
  import google.generativeai as genai
13
 
14
  # Setup
 
72
  script_or_style.decompose()
73
  return soup.get_text(separator='\n', strip=True)
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  else:
76
  return "Unsupported file format."
77
 
 
81
  # File uploader
82
  uploaded_file = st.file_uploader(
83
  "Upload a document or image",
84
+ type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"]
 
 
 
85
  )
86
 
87
  # Question input
 
126
  else:
127
  with st.spinner("Processing document..."):
128
  try:
 
 
129
  prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
130
  response = model.generate_content(prompt)
131
  st.success("💡 Answer:")
132
  st.write(response.text)
133
  except Exception as e:
134
+ st.error(f"Error generating answer: {e}")