Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import boto3 | |
| import re | |
| import os | |
| # Credenciales desde Secrets | |
| textract = boto3.client( | |
| "textract", | |
| region_name="us-east-1", | |
| aws_access_key_id="ASIA4LHG3BULWEOMKU4Z", | |
| aws_secret_access_key="C3fjhuRYrATatQPxt3V1/9S/30OegS1XhBFHgEq4", | |
| aws_session_token="IQoJb3JpZ2luX2VjEOL//////////wEaCXVzLXdlc3QtMiJIMEYCIQDeZo7cJ5Kaq++oYDKe7SmtmJuwzfg4wnB7ApcBoXo/8gIhAIvq0cMBl1ozGsZGhmDNn4G3MOM0AGTfvG6sqKB+kPbzKqwCCKv//////////wEQAhoMODQ4NzQwMTU4NzQzIgwkY6n0FrY0HkJuVGQqgAK/7MX2bIhDIY/bIQPzD0v8Teo4Wk4+1wXMvYMAFvVY1rIo4BicFF+aSKFRRuRZrbcJhcsgjT3lhDv+B6CbCfPdfdObipHxb79u2PUf/4IaBrK8yYpr2VG1epEP1OqoxMKYMEc04YTjPwFZIETWJiIp4ezOj0JIm5IJ+2Ku9zR6L6QlCAVaKldBu4ZzDUdzKGFPT2Pk8PTQyRsQHK8gsfvpbJO0cLY1m5LidorLOaTxd7pm95MMVLI+aUevEJGaEeAib+cMWcifL7RSZteWhU/sI8Fn/p0Lz29u2597v4NgQQFUNZIVNI9ErSzO0LQHbL29llSrry6b0ZDWqdswVy0sMJTIt88GOpwBZb191j8/0L2+K3PDEnroUysyAYJY4czZPM+Yqov0qTC39TWrGawPOA2jgyACwpRjnrF26Eb1DkCFqSGEDvHt5c+EErIQoDpGjjT2SvzMnTq1iDp8E0i7/DBN7RgXKViNhn9a+rlzZCWxV3EpDfLnRbp2lY++4jQI2O/xqOCpULFob9dbEiffIbTBoc/W9sYoFrWHvjqAimIHn/lV" | |
| ) | |
| total_acumulat = 0.0 | |
| def obtenir_total_tiquet(response): | |
| paraules_clau = [ | |
| "TOTAL", "TOTAL DUE", "IMPORT", "IMPORT TOTAL", | |
| "A PAGAR", "TOTAL A PAGAR", "AMOUNT", "AMOUNT DUE" | |
| ] | |
| linies = [] | |
| for bloc in response["Blocks"]: | |
| if bloc["BlockType"] == "LINE": | |
| linies.append(bloc["Text"]) | |
| for i, linia in enumerate(linies): | |
| text = linia.upper() | |
| if "SUBTOTAL" in text: | |
| continue | |
| for paraula in paraules_clau: | |
| if paraula in text: | |
| numeros = re.findall(r"\d+[.,]\d{2}", linia) | |
| if numeros: | |
| return numeros[-1] | |
| for j in range(1, 4): | |
| if i + j < len(linies): | |
| numeros = re.findall(r"\d+[.,]\d{2}", linies[i + j]) | |
| if numeros: | |
| return numeros[-1] | |
| return None | |
| def formatar_total(total_text): | |
| if total_text is None: | |
| return None | |
| total_text = total_text.replace(",", ".") | |
| try: | |
| return round(float(total_text), 2) | |
| except: | |
| return None | |
| def processar_tiquet(ruta_imatge): | |
| global total_acumulat | |
| try: | |
| with open(ruta_imatge, "rb") as f: | |
| imatge_bytes = f.read() | |
| response = textract.analyze_document( | |
| Document={"Bytes": imatge_bytes}, | |
| FeatureTypes=["FORMS"] | |
| ) | |
| total_text = obtenir_total_tiquet(response) | |
| total_num = formatar_total(total_text) | |
| if total_num is not None: | |
| total_acumulat += total_num | |
| return f"{total_num:.2f}", f"{total_acumulat:.2f}", "OK" | |
| else: | |
| return "No detectat", f"{total_acumulat:.2f}", "No s'ha pogut detectar" | |
| except Exception as e: | |
| return "Error", f"{total_acumulat:.2f}", str(e) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Extractor de totals de tiquets Toni Vivas") | |
| gr.Markdown( | |
| "Puja una imatge d'un tiquet de compra i l'aplicació extraura l'import total " | |
| "i anira acumulant els imports dels diferents tiquets." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| imatge_input = gr.Image( | |
| label="Puja una imatge del tiquet", | |
| type="filepath", | |
| height=420 | |
| ) | |
| with gr.Column(scale=1): | |
| import_detectat = gr.Textbox( | |
| label="Import detectat", | |
| interactive=False | |
| ) | |
| import_acumulat = gr.Textbox( | |
| label="Import acumulat", | |
| interactive=False | |
| ) | |
| estat = gr.Textbox( | |
| label="Estat", | |
| interactive=False | |
| ) | |
| boto = gr.Button("Analitzar") | |
| boto.click( | |
| fn=processar_tiquet, | |
| inputs=imatge_input, | |
| outputs=[import_detectat, import_acumulat, estat] | |
| ) | |
| demo.launch() |