Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize Groq client
|
| 6 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
""
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
completion = client.chat.completions.create(
|
| 17 |
-
model="
|
| 18 |
messages=[
|
| 19 |
{
|
| 20 |
"role": "user",
|
| 21 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
],
|
| 24 |
-
temperature=
|
| 25 |
-
max_completion_tokens=
|
| 26 |
top_p=1,
|
| 27 |
-
stream=
|
| 28 |
stop=None
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"
|
| 38 |
|
| 39 |
# Define Gradio interface
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=process_image_and_get_response,
|
| 42 |
-
inputs=
|
| 43 |
-
|
| 44 |
-
gr.Textbox(label="
|
|
|
|
| 45 |
],
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
description="Upload an image and optionally provide a description. The Groq AI will respond based on the description."
|
| 49 |
)
|
| 50 |
|
| 51 |
# Launch the interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from markdown import markdown
|
| 8 |
+
from bs4 import BeautifulSoup
|
| 9 |
+
import tempfile
|
| 10 |
|
| 11 |
# Initialize Groq client
|
| 12 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 13 |
|
| 14 |
+
def image_to_base64(image):
|
| 15 |
+
"""Convert PIL image to base64 string for Groq API."""
|
| 16 |
+
buffered = BytesIO()
|
| 17 |
+
image.save(buffered, format="JPEG") # Ou PNG si besoin
|
| 18 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 19 |
+
|
| 20 |
+
def parse_markdown_table_to_df(text):
|
| 21 |
+
"""Parse a Markdown table from text to Pandas DataFrame."""
|
| 22 |
+
html = markdown(text)
|
| 23 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 24 |
+
table = soup.find("table")
|
| 25 |
+
if not table:
|
| 26 |
+
return None # Pas de tableau trouvé
|
| 27 |
+
headers = [th.text for th in table.find_all("th")]
|
| 28 |
+
rows = [[td.text for td in tr.find_all("td")] for tr in table.find_all("tr")]
|
| 29 |
+
return pd.DataFrame(rows, columns=headers) if headers else pd.DataFrame(rows)
|
| 30 |
+
|
| 31 |
+
def process_image_and_get_response(image):
|
| 32 |
+
"""Process the uploaded image, send to Groq vision model, parse response to table, and generate Excel."""
|
| 33 |
+
if image is None:
|
| 34 |
+
return "Veuillez uploader une image.", None
|
| 35 |
+
|
| 36 |
+
# Convert image to base64
|
| 37 |
+
base64_image = image_to_base64(image)
|
| 38 |
+
|
| 39 |
+
# Prompt optimisé
|
| 40 |
+
prompt = "Extraie le tableau complet de cette image. Recopie-le à l'identique sous forme de tableau Markdown (avec des lignes | et --- pour les séparateurs). Assure-toi que les en-têtes et les lignes sont alignés correctement. Si l'image ne contient pas de tableau, dis-le explicitement."
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
completion = client.chat.completions.create(
|
| 44 |
+
model="llama-3.2-11b-vision-preview", # Modèle vision Groq
|
| 45 |
messages=[
|
| 46 |
{
|
| 47 |
"role": "user",
|
| 48 |
+
"content": [
|
| 49 |
+
{"type": "text", "text": prompt},
|
| 50 |
+
{
|
| 51 |
+
"type": "image_url",
|
| 52 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
|
| 53 |
+
}
|
| 54 |
+
]
|
| 55 |
}
|
| 56 |
],
|
| 57 |
+
temperature=0.5, # Plus bas pour plus de précision
|
| 58 |
+
max_completion_tokens=2048,
|
| 59 |
top_p=1,
|
| 60 |
+
stream=False, # Non-stream pour parsing facile
|
| 61 |
stop=None
|
| 62 |
)
|
| 63 |
|
| 64 |
+
response = completion.choices[0].message.content
|
| 65 |
+
|
| 66 |
+
# Parse la réponse en DataFrame
|
| 67 |
+
df = parse_markdown_table_to_df(response)
|
| 68 |
+
excel_file = None
|
| 69 |
+
if df is not None:
|
| 70 |
+
# Crée un fichier Excel temporaire
|
| 71 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 72 |
+
df.to_excel(tmp.name, index=False)
|
| 73 |
+
excel_file = tmp.name
|
| 74 |
+
|
| 75 |
+
return response, excel_file
|
| 76 |
+
|
| 77 |
except Exception as e:
|
| 78 |
+
return f"Erreur : {str(e)}", None
|
| 79 |
|
| 80 |
# Define Gradio interface
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=process_image_and_get_response,
|
| 83 |
+
inputs=gr.Image(type="pil", label="Uploader une image contenant un tableau"),
|
| 84 |
+
outputs=[
|
| 85 |
+
gr.Textbox(label="Réponse de l'IA (tableau Markdown)"),
|
| 86 |
+
gr.File(label="Télécharger le fichier Excel généré")
|
| 87 |
],
|
| 88 |
+
title="Extraction de Tableau depuis Image avec Groq et Export Excel",
|
| 89 |
+
description="Uploader une image avec un tableau. L'IA Groq l'extrait en Markdown, et un Excel est généré automatiquement."
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
# Launch the interface
|