Svngoku commited on
Commit
ce67523
·
verified ·
1 Parent(s): ce879d8
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -3,13 +3,12 @@ import base64
3
  import gradio as gr
4
  from mistralai import Mistral
5
  from mistralai.models import OCRResponse
6
- from mistralai.exceptions import MistralException
7
  from pathlib import Path
8
  from pydantic import BaseModel
9
  import pycountry
10
  import json
11
  import logging
12
- from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
13
  import tempfile
14
  from typing import Union, Dict, List
15
  from contextlib import contextmanager
@@ -32,7 +31,7 @@ class OCRProcessor:
32
  self.client = Mistral(api_key=self.api_key)
33
  try:
34
  self.client.models.list() # Validate API key
35
- except MistralException as e:
36
  raise ValueError(f"Invalid API key: {str(e)}")
37
 
38
  @staticmethod
@@ -52,19 +51,19 @@ class OCRProcessor:
52
  if os.path.exists(temp_file.name):
53
  os.unlink(temp_file.name)
54
 
55
- @retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry_if_exception_type=MistralException)
56
  def _call_ocr_api(self, document: Dict) -> OCRResponse:
57
  try:
58
  return self.client.ocr.process(model="mistral-ocr-latest", document=document)
59
- except MistralException as e:
60
  logger.error(f"OCR API call failed: {str(e)}")
61
  raise
62
 
63
- @retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry_if_exception_type=MistralException)
64
  def _call_chat_complete(self, model: str, messages: List[Dict], **kwargs) -> Dict:
65
  try:
66
  return self.client.chat.complete(model=model, messages=messages, **kwargs)
67
- except MistralException as e:
68
  logger.error(f"Chat complete API call failed: {str(e)}")
69
  raise
70
 
@@ -176,7 +175,7 @@ class OCRProcessor:
176
  def _format_structured_response(file_path: str, content: Dict) -> str:
177
  languages = {lang.alpha_2: lang.name for lang in pycountry.languages if hasattr(lang, 'alpha_2')}
178
  valid_langs = [l for l in content.get("languages", [DEFAULT_LANGUAGE]) if l in languages.values()]
179
-
180
  response = {
181
  "file_name": Path(file_path).name,
182
  "topics": content.get("topics", []),
@@ -195,7 +194,7 @@ def create_interface():
195
  placeholder="Enter your Mistral API key here",
196
  type="password"
197
  )
198
-
199
  def initialize_processor(api_key):
200
  try:
201
  processor = OCRProcessor(api_key)
 
3
  import gradio as gr
4
  from mistralai import Mistral
5
  from mistralai.models import OCRResponse
 
6
  from pathlib import Path
7
  from pydantic import BaseModel
8
  import pycountry
9
  import json
10
  import logging
11
+ from tenacity import retry, stop_after_attempt, wait_fixed
12
  import tempfile
13
  from typing import Union, Dict, List
14
  from contextlib import contextmanager
 
31
  self.client = Mistral(api_key=self.api_key)
32
  try:
33
  self.client.models.list() # Validate API key
34
+ except Exception as e:
35
  raise ValueError(f"Invalid API key: {str(e)}")
36
 
37
  @staticmethod
 
51
  if os.path.exists(temp_file.name):
52
  os.unlink(temp_file.name)
53
 
54
+ @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
55
  def _call_ocr_api(self, document: Dict) -> OCRResponse:
56
  try:
57
  return self.client.ocr.process(model="mistral-ocr-latest", document=document)
58
+ except Exception as e:
59
  logger.error(f"OCR API call failed: {str(e)}")
60
  raise
61
 
62
+ @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
63
  def _call_chat_complete(self, model: str, messages: List[Dict], **kwargs) -> Dict:
64
  try:
65
  return self.client.chat.complete(model=model, messages=messages, **kwargs)
66
+ except Exception as e:
67
  logger.error(f"Chat complete API call failed: {str(e)}")
68
  raise
69
 
 
175
  def _format_structured_response(file_path: str, content: Dict) -> str:
176
  languages = {lang.alpha_2: lang.name for lang in pycountry.languages if hasattr(lang, 'alpha_2')}
177
  valid_langs = [l for l in content.get("languages", [DEFAULT_LANGUAGE]) if l in languages.values()]
178
+
179
  response = {
180
  "file_name": Path(file_path).name,
181
  "topics": content.get("topics", []),
 
194
  placeholder="Enter your Mistral API key here",
195
  type="password"
196
  )
197
+
198
  def initialize_processor(api_key):
199
  try:
200
  processor = OCRProcessor(api_key)