import gradio as gr import torch import re import os auth_token = os.environ.get("HUGGING_FACE_HUB_TOKEN") from unsloth import FastLanguageModel instruction = """ From the given email, extract the following key values. The keys are explained below: * pickup_location: Street address of the origin location of goods. * pickup_cap: Postal code or ZIP code of the pickup location. * pickup_port: Port of pickup, often used in international shipping. * pickup_state: Only Country of pickup location. * delivery_location: Street address of the destination location of goods. * delivery_cap: Postal code or ZIP code of delivery location. * delivery_port: Port of delivery, similar to pickup port. * delivery_state: State or region of delivery location. * total_quantity: Overall quantity of shipped items (e.g., pieces, boxes). Calculate the total_quantity by summing the quantity of all packages. * total_weight: Total weight of the shipment (e.g., kg, lbs). Calculate the total_weight by summing the weights of all packages. * total_volume: Total volume of the shipment (e.g., cubic meters, cubic feet). Calculate the total_volume by summing the volumes of all packages. * quantity: Individual Quantity of a specific item being shipped. * package_type: Individual Type of packaging used (e.g., pallets, cartons). * weight: Individual Weight of a specific package. * measures: Individual Dimensions or measurements of a package. * stackable: Indicates whether the shipment is stackable (True or False). * volume: Individual Volume of a specific package. * commodity: Type of goods or commodities being shipped. * company: Name of the email sending company, also the shipping company or carrier. * incoterms: Choose available options: EXW, FCA, FAS, FOB, CFR, CIF, CPT, CIP, DAP, DPU, DDP. """ def process_output(output): """ Process the output to extract the response. """ # Define the regex pattern pattern = r'### Response:\n?(.*?)<\|endoftext\|>' # Search for the pattern in the output match = re.search(pattern, output, re.DOTALL) if match: # Extract the response response = match.group(1) # Remove specified symbols cleaned_str = re.sub(r'\\n|\\\\|\\\'', '', response) return cleaned_str else: return output # Define the function for generating output based on input def generate_output(input_text,model): # Prompt for the instruction output = "" # Initialize the FastLanguageModel model, tokenizer = FastLanguageModel.from_pretrained( model_name = model, max_seq_length = 4096, dtype = None, load_in_4bit = True, ) FastLanguageModel.for_inference(model) # Enable native 2x faster inference alpaca_prompt = f""" Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. ### Instruction: {instruction} ### Input: {input_text} ### Response: """ # Tokenize the input text inputs = tokenizer([alpaca_prompt], return_tensors="pt").to("cuda") # Generate outputs outputs = model.generate(**inputs, max_new_tokens=4096, use_cache=True) output = tokenizer.batch_decode(outputs) cleaned_response = process_output(output) return output,cleaned_response examples = [ ["""dubai", "Dear Team, ; Please quote SEA option for below inquiry ; POD :Jebel Ali . ; estimated packing details ; 3300x950x1350 mm ; 3800x1700x1350 mm ; 1900x2000x1200 mm ; 1900x2000x1200 mm ; 2600x1300x1200 mm ; 3900x1100x1350 mm ; 820x780x950 mm ; Total weight kg 5000 approx ; Not stackable ; Marrone Srl ; via Rui, 5 - Loc. Prà dei Risi ; I - 33080 Zoppola (PN) ; Warehouse: 08:00 – 12:00 / 13:00 – 16:30 ; Warehouse Handling + Free days w.e.f 01-01-2024 ; AED 250 + vat until 10 CBM - AED 350 + Vat 11 CBM on wards ; Free time applicable - 7 days only for General cargo from DO readiness & No free time for HAZ CARGO, post which standard storage tariff will apply. ; Important Notice ; : Considering the ongoing circumstances, please be informed after booking confirmation the rates and schedules may undergo changes without advance notice. ; Please also expect vessel delays and changes in transit time. ; CSS will not be liable for any claims that arise due to this. ; Deepak Unnikrishnan ; Sr Sales Coordinator ; Consolidated Shipping Service L.L.C. ; Office: +971 4 883 1303 | Ext: 1163| Toll Free: 800277 | Mobile: +971501513697| ; Dubai | United Arab Emirates ; nvo-import5@cssdubai.com | www.cssgroupsite.com ; Our Offices: Dubai | Abu Dhabi | Sharjah | Ras-Al-Khaimah | Bahrain | Oman | Qatar | Saudi Arabia | Kuwait | Iraq | Africa | Turkiye | India | Sri Lanka | ; Disclaimer: This email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please delete all copies and notify the sender immediately. ; Please read our ; disclaimer."""], ["""I: LCL BUSAN RICHIESTA QUOTAZIONE", "Buongiorno, ; Ci servirebbe cortesemente Vs quotazione per: ; PESO: 7140 ; COLLI: 7 Pallet ; MISURE: 100X120X130H (6 PLTS) 100X120X160H (1 PLT) ; La dogana verrà fatta a stabilimento, pertanto si richiede solo lo scarico bolla. ; La attendiamo ; Grazie mille ; Nicolo Esibiti ; Export Sales Department ; T +39 010 2717399 ; Casasco & Nardi S.p.A. ; Palazzo Andrea Pitto ; Piazza Fossatello, 3 ; 16124, Genova ; www.casasconardi.com ; """], ["""I: CIC/2324/806 // Rates Ex Italy To Chennai Port", "Dear Ceriana , ; Please quote your best rate Ex-Works Cesena Italy, By Sea to Chennai Sea Port for below given details : ; Cargo is non stackable and non tiltable, send quote accordingly. ; ITEM - EQUIPMENT FOR PHYSICAL EXERCISE ; Total - 10 Packages ; Net Weight - 1838.62 Kgs ; Total Weight - 2291.85 Kgs ; Total Volume (m3) - 17.36 ; Refer attached Packing List for Dimensions, etc. ; Pick-Up Address : ; Technogym S.P.A ; Via Calcinaro, 2861 ; 47521 Cesena ( FC) ; Italy ; Thanks & Regards, ; RAKSHANA.R ; A Group of RADAR VENTURES PVT LTD. ; 6th Floor, Menon Eternity, St. Marys Road, ; Alwarpet, Chennai-600018 ; PH NO:7305888727 ; MAIL ID : ; rakshana.r@magikcargo.com"""], ["""I: KEELUNG", "Ciao Paolo, ; Per favore mi quoti per: ; Winery: Cantina Tollo ; pick up::66010 ; POD: Keelung ; Cargo size : 120x 100x190 cm/ 2 Pallets 1581/Kg ; 210 cases ,1581kg, 2Pallets about 4.56 CBM ; Grazie ; Cordiali saluti/ Best regards ; ________________________________________________________________________________ ; Franco Raiola – Area manager ; Cargo Compass spa ; Via Lavoria 56/L/M/N | Cenaia | Crespina Lorenzana | Pisa | 56040 | ITALY ; fraiola@cargocompass.it | O +39.050.643133 | F +39.050.644687 | C+39.335.1277205 | www.cargocompassworld.com"""], ["""I: LAINATE > KEE", "Ciao Paolo / Giorgia, ; Per favore mi quotate da LAINATE (MI) a KEELUNG ; (MOBILI) ; 5 colli – 168,7 kg ; 120x80x187 ; 265x100x78 ; 120x53x54 ; 78x82x78 ; 63x56x100 ; Grazie ciao ; Alberto Porta ; MOBILE NUMBER + 39 3337934630 ; T: +39 0331 1090068 ; E: alberto.porta@nordestgroup.it ; W: www.nordestgroup.it ; Branch MXP: Via del Gregge n°100 – 21015 Lonate Pozzolo ( VA ) ; IATA code 38471480003 – PI/VAT 01327440937 – SDI M5UXCR1 ; Headquarters: Viale Ponte Rosso 19 - 33078 San Vito al Tagliamento ( PN ) ; Branch GOA: Piazza della Nunziata n°5 - 16124 Genova ( GE )"""], ["""I: RATE EXW FRANCE - BUENOS AIRES LCL qt3218", "Dear Davide, ; Is it possible coordinate this cargo for Genova? ; PICK UP LOCATION: ; 6 rue Désir Prévost – La grande Brénche ; 91070 Bondoufle FRANCE ; DESTINATION: BUENOS AIRES ; CARGO : ; 1 pallet / 120x80x123 cm – 124 kgs ; 1 pallet / 120x80x108 cm – 108 kgs ; Caga general ; Brgds, ; Estèfany"""], ["""I: Richiesta quotazione da 45030 Occhiobello (RO) / Muscat + schedule + agente", "Buongiorno a tutti, ; avremmo bisogno dei vostri costi come da oggetto per 2 pallets – 1075 kg tot – cm 120x100x165/2 ; commodity egg yolk powder ; prego gentilmente fornire, oltre ai dettagli del servizio e t/t, anche schedule con cut off e nominativo del vostro agente a destino ; grazie e buon lavoro ; Cordiali Saluti / Best Regards ; Vera Mangini ; Ocean Export/Import Customer Service ; YUHANG ITALIA S.R.L. ; Office phone: +39 02 51650641 ; Office Address: Via Zara 1, Pioltello (MI) 20096 ; Warehouse Address: Via Gorizia 4/B, Pioltello (MI) 20096 ; www.yuhang.it"""], ["""I: Richiesta quotazione per Chennai", "Buon pomeriggio ; Preghiamo Vs quotazione per la seguente merce: ; Merce: almond paste /pasta di mandorla ; 1 pedana cm 100x120 x 136 cm h ; 1 pedana cm 100x120 x 96 cm h ; Peso: netto 1.000 kg / peso lordo 1095 kg ; H.S. code 20071099 ; NON SOVRAPPONIBILI. ; Da ritirare a 33031 Basiliano (UD) ; POD: Chennai / India ; Grazie e saluti ; Dario Masiello ; Francesco Parisi Casa di Spedizioni S.p.A. ; Overseas and Commodities - Export Manager ; Headquarters Trieste, Italy ; Viale Miramare, 5, 34135 Trieste - Italy ; tel. +39 040 4193140 ; web site: www.francescoparisi.com"""], ["""I: RICHIESTA OFFERTA CFR CHITTOGRAM 6 PLT 8166 KG NO ADR", "Buon pomeriggio, ; chiedo cortesemente vs offerta per ; CFR CHITTOGRAM ; n. 5 pallet – 130x110x200h ; n. 1 pallet – 130x110x50h ; kg. 8166 ; no ADR ; Ritiro in linea: VERDELLINO (BG) ; Grazie ; Deborah Corbetta ; Sales Representative ; Savino Del Bene S.p.A. – Bergamo ; Airport Center ; Via Orio 29 d/e ; 24050 Grassobbio (BG) ; + 39 035 586 3011 int. 322 – cell. 331 6573947 ; deborah.corbetta@savinodelbene.com ; www.savinodelbene.com"""], ["""I: IN-38521 // ITALIA- MEX // LCL VAN BERKEL", "Gentilissimi, ; mio agente Messicano (Inndingo) ha negoziato con la Vs. Messicana (Mewtral Mexico) nolo in Collect. ; Mi occorrono costi di ritiro+magazzino+operazione doganale per: ; 1plts cm 83*71*83 - 64 kg - ; Destino Veracruz CFS ; Ritiro ; Van Berkel International Srl ; C/O Interlaziale ; Via Milano 10 ; Ceriano Laghetto (MB) – 20816 ; Italia ; Orario magazzino: 8.30-12.30 / 13.30-17.00 – DA LUNEDI A VENERDI ; Flete marítimo en términos collect con mi contrato con la consolidadora Sinergy Cargo (Coloader Newtral Mexico). ; Compárteme tus costos de recolección, handling, despacho aduanal, costo de EUR-1, y costos pertinentes a este envío, así como próximas salidas, gracias! ; Se occorrono altri dati rimango a disposizione. ; Alessio"""], ["""gorla minore (va) + jebel ali/u.a.e.", "ciao ale ; ; 1 plt/kg 415 ; cm 80*120*90 ; ; prego quotami da gorla minore (va) su jebel ali ; ; grazie ; Flavio Greco ; O.T.E. Spedizioni Internazionali S.r.l. ; Via Pasubio 3 , 20026 Novate Milanese (Mi) ; Mb.+ 39 350.1490.834 ; I dati personali verranno gestiti ai sensi del D.Lgs 196/2003 (tutela della ; privacy) ; Personal date supplied to us will be handled according the Italian law D.Lgs ; 196/2003 (privacy protection act) ;"""], ["""bizzarone (co) + yakima/u.s.a.", "ciao ale ; ; 2 plts/totale kg 1.200 ; cm 80x120x220/cad ; .reti agricole ; ; prego quotami da bizzarone (co) su 98903 dap yakima, wa ; ; grazie ; Flavio Greco ; O.T.E. Spedizioni Internazionali S.r.l. ; Via Pasubio 3 , 20026 Novate Milanese (Mi) ; Mb.+ 39 350.1490.834 ; I dati personali verranno gestiti ai sensi del D.Lgs 196/2003 (tutela della ; privacy) ; Personal date supplied to us will be handled according the Italian law D.Lgs ; 196/2003 (privacy protection act) ; """], ["""SINERGY / SAN JOSE' COSTA RICA LCL", "Ciao ; Prego quotare : ; LUOGO DI CARICO: ; 31024 Ormelle TV Italy ; DATA CARICO: 07/12 ; MERCE: ; 3 PLT 100*120*220 H NON SOVR ; + ; 1 PLT 80*120*50 H NON SOVR ; PESO: KG 2700 ; RESA CIF SAN JOSE / COSTARICA ; DOGANA NS CURA ; SCARICO BOLLA VS CURA ; VGM VS CURA ; TRANSIT TIME ; PROSSIME PARTENZE ; GRAZIE ; Best regards, ; Stefano Tonello, Sales Representative, Road, Air & Sea, Italy ; Sales ; [DSV logo]Global Transport and Logistics ; DSV S.p.A. | Corso Nuova Zelanda 8 | Padua | 35127 | Italy ; stefano.tonello@it.dsv.com | Office +3= ; 9 0498692407 | Mobile +39 3357808789 | www.dsv.com ; DSV Standard Terms and Conditions ; All services are rendered according to the DSV Standard Terms and Condition= ; s and the Standard Trading Conditions (FEDESPEDI). In case of contradiction= ; s between the DSV Standard Terms and Conditions and the FEDESPEDI, the FEDE= ; SPEDI shall prevail. Your legal position is materially altered due to DSV's= ; limited liability in case of loss of, damage to or delay of your cargo. DS= ; V will furthermore obtain the right of lien over your cargo and all claims = ; against DSV are time-barred after 1 year. We recommend that you review the = ; full text of the DSV Standard Terms and Conditions and the FEDESPEDI prior = ; to DSV's pick-up of your cargo - FEDESPEDI - DSV Standard Terms & Conditions. ; Orders undertaken as carrier of overseas carriage are subject to conditions= ; stipulated in the DSV Ocean Transport Bill of Lading/Sea Waybill. Your leg= ; al position is materially altered due to DSV's limited liability in case of= ; loss of, damage to or delay of your cargo. DSV will furthermore obtain the= ; right of lien over your cargo and all claims against DSV are time-barred a= ; fter 9 months. We recommend that you review the full version of the DSV Oce= ; an Bill of Lading before DSV's pick-up of your cargo - DSV Ocean Transport = ; B/L ; Orders undertaken as carrier of carriage by air are subject to conditions s= ; tipulated in DSV's House Air waybill. Your legal position is materially alt= ; ered due to DSV's limited liability in case of loss of, damage to or delay = ; of your cargo. All claims against DSV are time-barred after 2 years. We rec= ; ommend that you review the full version of the DSV House Air waybill prior = ; to DSV's pick-up of your cargo - DSV House Air Waybill ; In case of discrepancy between the DSV Standard Terms and Conditions and th= ; e terms stipulated in the DSV Ocean Transport B/L or the DSV House Air Wayb= ; ill, the terms of the DSV Ocean Transport B/L or the DSV House Air Waybill = ; shall prevail. ; DSV processes your personal data according to DSV General Privacy Informati= ; on Notice. ; Best regards, ; Stefano Tonello, Sales Representative, Road, Air & Sea, Italy ; stefano.tonello@it.dsv.com | Office +3= ; 9 0498692407 | Mobile +39 3357808789 | www.dsv.com ; """], ["""quotazione Nhava Sheva Bari", "ciao ; ; per favore mi quoti ; ; pod Nhava Sheva ; nolo collect ; ritiro a 70123 Bari ; ; no.1 cassa kg 362 cm 167x86x91 ; ; grazie ; ; Michela Viele ; O.T.E. ; Spedizioni Internazionali S.r.l. ; Via Pasubio 3 , 20026 Novate Milanese ( Milano ) ; Ph. + 39 02 662471 ; I dati personali verranno gestiti ai sensi del D.Lgs 196/2003 (tutela della ; privacy) Personal date supplied to us will be handled according the Italian ; law D.Lgs 196/2003 (privacy protection act ; ; """], ["""";; mi fai avere dap per tratta in oggetto.;;si tratta di tutta merce non sovrapponibile composta da;;1 cassa - kg 365 - dimensioni cm 210x145x150;1 cassa - kg 285 - dimensioni cm 290x170x115;1 cassa - kg 330 - dimensioni cm 300x180x115;;;fatemi avere costi considerando :;;- preavviso di consegna al destinatario;- sponda idraulica per la consegna a destino;;; e ;angela;;[logo firma1];;gava ifc spa;"""], ["""ale;; ;;2 plts/kg 420;;cm 100x175x175/cad;; ;; quotami da cernusco sul naviglio (mi) su hamad;; ;;;;"""], ["""export LCL Uruguay/Montevideo", "Ciao Lara, ; ; mi puoi dare la quotazione compreso ritiro delle merci al deposito Tuscanlogistics/Montale (PT) ; merce pronta ; prego indicare le partenze navi disponibili ; ; POD: MONTEVIDEO / URUGUAY ; ; Quantità merce: 40 scatole di cartone in 7 pallets ; PLT 1) cm. 120 x 80 x 190 >>> kg. 224,00 ; PLT 2) cm. 120 x 80 x 190 >>> kg. 212,00 ; PLT 3) cm. 120 x 80 x 150 >>> kg. 129,00 ; PLT 4) cm. 120 x 80 x 170 >>> kg. 163,00 ; PLT 5) cm. 120 x 80 x 160 >>> kg. 233,00 ; PLT 6) cm. 120 x 80 x 160 >>> kg. 180,00 ; PLT 7) cm. 120 x 80 x 170 >>> kg. 147,00 ; ; pallets epal fumigati ; tipo di merce abbigliamento per donna ; unico ricevitore, resa CFR Montevideo: ; ; Indirizzo di ritiro: ; TUSCANLOGISTICS SRL ; Via Enrico Mattei, 18 ; 51037 MONTALE (PT) ; TEL. 0573 959599 ; a disposizione per ogni eventuale informazione ; grazie per l'attenzione ; cordiali saluti ; Massimiliano Bardi"""] ] model_options = ["sxandie/llama_3_8b_4bitQ","DataIntelligenceTeam/NER-Phi-3-mini-4k-instruct"] #,"DataIntelligenceTeam/NER-gemma-7b-bnb-4bit","DataIntelligenceTeam/llama-3-8b-Instruct-bnb-4bit","DataIntelligenceTeam/mistral-7b-instruct-v0.2-bnb-4bit"] inputs = [ gr.inputs.Textbox(label="Input Text"), gr.inputs.Dropdown(label="Model", choices=model_options, default=model_options[1]) ] outputs = [ gr.outputs.Textbox(label="Original Output Text"), gr.outputs.Textbox(label="Formatted JSON") ] # Create Gradio interface iface = gr.Interface(fn=generate_output, inputs=inputs, outputs= outputs, examples=examples, title="Email Information Extraction", description="Extract key information from the provided email.") iface.launch()