Spaces:
Running
Running
Fixing issues made by grok
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ 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
|
@@ -30,9 +30,8 @@ class OCRProcessor:
|
|
30 |
raise ValueError("API key must be provided")
|
31 |
self.api_key = api_key
|
32 |
self.client = Mistral(api_key=self.api_key)
|
33 |
-
# Test API key validity on initialization
|
34 |
try:
|
35 |
-
self.client.models.list() #
|
36 |
except MistralException as e:
|
37 |
raise ValueError(f"Invalid API key: {str(e)}")
|
38 |
|
@@ -53,7 +52,7 @@ class OCRProcessor:
|
|
53 |
if os.path.exists(temp_file.name):
|
54 |
os.unlink(temp_file.name)
|
55 |
|
56 |
-
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry_if_exception_type
|
57 |
def _call_ocr_api(self, document: Dict) -> OCRResponse:
|
58 |
try:
|
59 |
return self.client.ocr.process(model="mistral-ocr-latest", document=document)
|
@@ -61,7 +60,7 @@ class OCRProcessor:
|
|
61 |
logger.error(f"OCR API call failed: {str(e)}")
|
62 |
raise
|
63 |
|
64 |
-
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2), retry_if_exception_type
|
65 |
def _call_chat_complete(self, model: str, messages: List[Dict], **kwargs) -> Dict:
|
66 |
try:
|
67 |
return self.client.chat.complete(model=model, messages=messages, **kwargs)
|
@@ -191,14 +190,12 @@ def create_interface():
|
|
191 |
gr.Markdown("# Mistral OCR & Structured Output App")
|
192 |
gr.Markdown("Enter your Mistral API key below to use the app. Extract text from PDFs and images or get structured JSON output.")
|
193 |
|
194 |
-
# API Key input
|
195 |
api_key_input = gr.Textbox(
|
196 |
label="Mistral API Key",
|
197 |
placeholder="Enter your Mistral API key here",
|
198 |
type="password"
|
199 |
)
|
200 |
|
201 |
-
# Function to initialize processor with API key
|
202 |
def initialize_processor(api_key):
|
203 |
try:
|
204 |
processor = OCRProcessor(api_key)
|
@@ -208,11 +205,9 @@ def create_interface():
|
|
208 |
except Exception as e:
|
209 |
return None, f"**Error:** Unexpected error: {str(e)}"
|
210 |
|
211 |
-
# Store processor state
|
212 |
processor_state = gr.State()
|
213 |
api_status = gr.Markdown("API key not set. Please enter and set your key.")
|
214 |
|
215 |
-
# Button to set API key
|
216 |
set_api_button = gr.Button("Set API Key")
|
217 |
set_api_button.click(
|
218 |
fn=initialize_processor,
|
@@ -232,7 +227,7 @@ def create_interface():
|
|
232 |
with gr.Tab(name):
|
233 |
if input_type == gr.Textbox:
|
234 |
inputs = input_type(label=label, placeholder=f"e.g., https://example.com/{label.lower().replace(' ', '')}")
|
235 |
-
else:
|
236 |
inputs = input_type(label=label, file_types=file_types)
|
237 |
output = gr.Markdown(label="Result")
|
238 |
button_label = name.replace("OCR with ", "").replace("Structured ", "Get Structured ")
|
|
|
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
|
|
|
30 |
raise ValueError("API key must be provided")
|
31 |
self.api_key = api_key
|
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 |
|
|
|
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)
|
|
|
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)
|
|
|
190 |
gr.Markdown("# Mistral OCR & Structured Output App")
|
191 |
gr.Markdown("Enter your Mistral API key below to use the app. Extract text from PDFs and images or get structured JSON output.")
|
192 |
|
|
|
193 |
api_key_input = gr.Textbox(
|
194 |
label="Mistral API Key",
|
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)
|
|
|
205 |
except Exception as e:
|
206 |
return None, f"**Error:** Unexpected error: {str(e)}"
|
207 |
|
|
|
208 |
processor_state = gr.State()
|
209 |
api_status = gr.Markdown("API key not set. Please enter and set your key.")
|
210 |
|
|
|
211 |
set_api_button = gr.Button("Set API Key")
|
212 |
set_api_button.click(
|
213 |
fn=initialize_processor,
|
|
|
227 |
with gr.Tab(name):
|
228 |
if input_type == gr.Textbox:
|
229 |
inputs = input_type(label=label, placeholder=f"e.g., https://example.com/{label.lower().replace(' ', '')}")
|
230 |
+
else:
|
231 |
inputs = input_type(label=label, file_types=file_types)
|
232 |
output = gr.Markdown(label="Result")
|
233 |
button_label = name.replace("OCR with ", "").replace("Structured ", "Get Structured ")
|