Belemort commited on
Commit
0bb5fec
1 Parent(s): d21d31c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -67
app.py CHANGED
@@ -4,23 +4,11 @@ from langchain_community.tools import TavilySearchResults, JinaSearch
4
  import concurrent.futures
5
  import json
6
  import os
7
-
8
- UPLOAD_FOLDER = 'uploads'
9
- if not os.path.exists(UPLOAD_FOLDER):
10
- os.makedirs(UPLOAD_FOLDER)
11
- UPLOAD_FOLDER = 'static'
12
- if not os.path.exists(UPLOAD_FOLDER):
13
- os.makedirs(UPLOAD_FOLDER)
14
-
15
  import arxiv
16
- import fitz # PyMuPDF
17
  from docx import Document
18
  from PIL import Image
19
  import io
20
  import base64
21
- import mimetypes
22
-
23
-
24
 
25
  # Set environment variables for Tavily API
26
  os.environ["TAVILY_API_KEY"] = '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'
@@ -34,55 +22,26 @@ client_3 = Mistral(api_key='cvyu5Rdk2lS026epqL4VB6BMPUcUMSgt')
34
  def encode_image_bytes(image_bytes):
35
  return base64.b64encode(image_bytes).decode('utf-8')
36
 
37
- # Functions to process various file types
38
- def process_file(file_path):
39
- mime_type, _ = mimetypes.guess_type(file_path)
40
- if mime_type == 'application/pdf':
41
- return process_pdf(file_path)
42
- elif mime_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
43
- return process_docx(file_path)
44
- elif mime_type == 'text/plain':
45
- return process_txt(file_path)
46
- else:
47
- print(f"Unsupported file type: {mime_type}")
48
- return None, []
49
-
50
- def process_pdf(file_path):
51
- text = ""
52
- images = []
53
- pdf_document = fitz.open(file_path)
54
- for page_num in range(len(pdf_document)):
55
- text += pdf_document[page_num].get_text("text")
56
- for _, img in enumerate(pdf_document.get_page_images(page_num, full=True)):
57
- xref = img[0]
58
- base_image = pdf_document.extract_image(xref)
59
- image_bytes = base_image["image"]
60
- image_ext = base_image["ext"]
61
- base64_image = encode_image_bytes(image_bytes)
62
- image_data = f"data:image/{image_ext};base64,{base64_image}"
63
- images.append({"type": "image_url", "image_url": image_data})
64
- return text, images
65
-
66
- def process_docx(file_path):
67
- doc = Document(file_path)
68
- text = ""
69
  images = []
70
- for paragraph in doc.paragraphs:
71
- text += paragraph.text + "\n"
72
- for rel in doc.part.rels.values():
73
- if "image" in rel.target_ref:
74
- img_data = rel.target_part.blob
75
- img = Image.open(io.BytesIO(img_data))
76
- buffered = io.BytesIO()
77
- img.save(buffered, format="JPEG")
78
- image_base64 = encode_image_bytes(buffered.getvalue())
79
- images.append({"type": "image_url", "image_url": f"data:image/jpeg;base64,{image_base64}"})
80
- return text, images
81
-
82
- def process_txt(file_path):
83
- with open(file_path, "r", encoding="utf-8") as file:
84
- text = file.read()
85
- return text, []
86
 
87
  # Search setup function
88
  def setup_search(question):
@@ -204,11 +163,8 @@ def ask_question_to_mistral(text, question, images=[]):
204
  return response.choices[0].message.content
205
 
206
  # Gradio interface
207
- def gradio_interface(file, task, question, compression_percentage):
208
- if file:
209
- text, images = process_file(file.name)
210
- else:
211
- text, images = "", []
212
 
213
  topics, articles_json = init(text, images)
214
 
@@ -225,7 +181,8 @@ def gradio_interface(file, task, question, compression_percentage):
225
  with gr.Blocks() as demo:
226
  gr.Markdown("## Text Analysis: Summarization or Question Answering")
227
  with gr.Row():
228
- file_input = gr.File(label="Upload File")
 
229
  task_choice = gr.Radio(["Summarization", "Question Answering"], label="Select Task")
230
  question_input = gr.Textbox(label="Question (for Question Answering)", visible=False)
231
  compression_input = gr.Slider(label="Compression Percentage (for Summarization)", minimum=10, maximum=90, value=30, visible=False)
@@ -238,6 +195,6 @@ with gr.Blocks() as demo:
238
  result_output = gr.JSON(label="Results")
239
 
240
  submit_button = gr.Button("Submit")
241
- submit_button.click(gradio_interface, [file_input, task_choice, question_input, compression_input], result_output)
242
 
243
  demo.launch()
 
4
  import concurrent.futures
5
  import json
6
  import os
 
 
 
 
 
 
 
 
7
  import arxiv
 
8
  from docx import Document
9
  from PIL import Image
10
  import io
11
  import base64
 
 
 
12
 
13
  # Set environment variables for Tavily API
14
  os.environ["TAVILY_API_KEY"] = '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'
 
22
  def encode_image_bytes(image_bytes):
23
  return base64.b64encode(image_bytes).decode('utf-8')
24
 
25
+ # Function to decode base64 images
26
+ def decode_base64_image(base64_str):
27
+ image_data = base64.b64decode(base64_str)
28
+ return Image.open(io.BytesIO(image_data))
29
+
30
+ # Process text and images provided by the user
31
+ def process_input(text_input, images_base64):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  images = []
33
+ if images_base64:
34
+ for img_data in images_base64:
35
+ try:
36
+ img = decode_base64_image(img_data)
37
+ buffered = io.BytesIO()
38
+ img.save(buffered, format="JPEG")
39
+ image_base64 = encode_image_bytes(buffered.getvalue())
40
+ images.append({"type": "image_url", "image_url": f"data:image/jpeg;base64,{image_base64}"})
41
+ except Exception as e:
42
+ print(f"Error decoding image: {e}")
43
+
44
+ return text_input, images
 
 
 
 
45
 
46
  # Search setup function
47
  def setup_search(question):
 
163
  return response.choices[0].message.content
164
 
165
  # Gradio interface
166
+ def gradio_interface(text_input, images_base64, task, question, compression_percentage):
167
+ text, images = process_input(text_input, images_base64)
 
 
 
168
 
169
  topics, articles_json = init(text, images)
170
 
 
181
  with gr.Blocks() as demo:
182
  gr.Markdown("## Text Analysis: Summarization or Question Answering")
183
  with gr.Row():
184
+ text_input = gr.Textbox(label="Input Text")
185
+ images_base64 = gr.Textbox(label="Base64 Images (comma-separated, if any)", placeholder="data:image/jpeg;base64,...", lines=2)
186
  task_choice = gr.Radio(["Summarization", "Question Answering"], label="Select Task")
187
  question_input = gr.Textbox(label="Question (for Question Answering)", visible=False)
188
  compression_input = gr.Slider(label="Compression Percentage (for Summarization)", minimum=10, maximum=90, value=30, visible=False)
 
195
  result_output = gr.JSON(label="Results")
196
 
197
  submit_button = gr.Button("Submit")
198
+ submit_button.click(gradio_interface, [text_input, images_base64, task_choice, question_input, compression_input], result_output)
199
 
200
  demo.launch()