Felipe Silva commited on
Commit
ece1514
·
1 Parent(s): d4a1668

Ajuste doc converter

Browse files
Files changed (2) hide show
  1. app.py +6 -14
  2. utils.py +7 -3
app.py CHANGED
@@ -10,14 +10,9 @@ import config
10
  zero = torch.Tensor([0]).cuda()
11
  print(zero.device) # <-- 'cpu' 🤔
12
 
13
- # @spaces.GPU
14
- # def greet(n):
15
- # print(zero.device) # <-- 'cuda:0' 🤗
16
- # return f"Hello {zero + n} Tensor"
17
-
18
  MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
19
 
20
- name_model = "Qwen/Qwen2.5-7B-Instruct-GPTQ-Int8" #"TinyLlama/TinyLlama-1.1B-Chat-v1.0"
21
  config.local_model_path = snapshot_download(
22
  repo_id=name_model,
23
  cache_dir="/root/.cache/huggingface",
@@ -37,14 +32,11 @@ def process_file(file):
37
  if file_size > MAX_FILE_SIZE:
38
  return f"O arquivo excede o limite. Por favor, realize o upload de um arquivo que contenha no máximo {MAX_FILE_SIZE/1024/1024:.1f}MB."
39
 
40
- file_obj, type_file = fix_type(file)
41
- texto_extraido = None
42
-
43
- if type_file in EXTENSIONS_FILES:
44
- texto_extraido = extract_content_in_pdf(file_obj)
45
- elif type_file in EXTENSIONS_IMG_FILES:
46
- texto_extraido = doc_converter(file)
47
- return texto_extraido or "Não foi possível extrair texto."
48
 
49
  @spaces.GPU
50
  def ask_question(texto_extraido, question):
 
10
  zero = torch.Tensor([0]).cuda()
11
  print(zero.device) # <-- 'cpu' 🤔
12
 
 
 
 
 
 
13
  MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
14
 
15
+ name_model = "Qwen/Qwen2.5-7B-Instruct-GPTQ-Int8"
16
  config.local_model_path = snapshot_download(
17
  repo_id=name_model,
18
  cache_dir="/root/.cache/huggingface",
 
32
  if file_size > MAX_FILE_SIZE:
33
  return f"O arquivo excede o limite. Por favor, realize o upload de um arquivo que contenha no máximo {MAX_FILE_SIZE/1024/1024:.1f}MB."
34
 
35
+ texto_extraido = doc_converter(file)
36
+ if texto_extraido is None:
37
+ return "Não foi possível extrair o texto."
38
+
39
+ return texto_extraido
 
 
 
40
 
41
  @spaces.GPU
42
  def ask_question(texto_extraido, question):
utils.py CHANGED
@@ -21,9 +21,13 @@ def fix_type(file_upload):
21
 
22
  @spaces.GPU
23
  def doc_converter(file_path):
24
- converter = DocumentConverter()
25
- result = converter.convert(file_path)
26
- return result.document.export_to_markdown()
 
 
 
 
27
 
28
  # Resize image while maintaining aspect ratio
29
  def resize_image(image, max_size):
 
21
 
22
  @spaces.GPU
23
  def doc_converter(file_path):
24
+ try:
25
+ converter = DocumentConverter()
26
+ result = converter.convert(file_path)
27
+ return result.document.export_to_markdown()
28
+ except Exception as ex:
29
+ print(ex)
30
+ return None
31
 
32
  # Resize image while maintaining aspect ratio
33
  def resize_image(image, max_size):