Jyotiyadav commited on
Commit
fb7af29
·
verified ·
1 Parent(s): 61e011d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -79
app.py CHANGED
@@ -5,91 +5,70 @@ import torchvision
5
  from transformers import pipeline
6
  auth_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
7
 
8
- # Function to generate output based on input
9
- def generate_output(input_text,max_new_tokens, temperature, top_k, top_p, model):
10
- # Initialize the pipeline
11
- pipe = pipeline(
12
- "text-generation",
13
- model=model,
14
- tokenizer = model,
15
- torch_dtype=torch.bfloat16,
16
- device_map="auto"
17
- )
18
 
19
- prompt = f"""
20
- Your task is to extract the information corresponding to the provided labels from the below given email.
21
-
22
- ### Instruction:
23
- * pickup_location: Street address of the origin location of goods.
24
- * pickup_cap: Postal code or ZIP code of the pickup location.
25
- * pickup_port: Port of pickup, often used in international shipping.
26
- * pickup_state: Only Country of pickup location.
27
- * delivery_location: Street address of the destination location of goods.
28
- * delivery_cap: Postal code or ZIP code of delivery location.
29
- * delivery_port: Port of delivery, similar to pickup port.
30
- * delivery_state: State or region of delivery location.
31
- * total_quantity: Overall quantity of shipped items (e.g., pieces, boxes). Calculate the total_quantity by summing the quantity of all packages.
32
- * total_weight: Total weight of the shipment (e.g., kg, lbs). Calculate the total_weight by summing the weights of all packages.
33
- * total_volume: Total volume of the shipment (e.g., cubic meters, cubic feet). Calculate the total_volume by summing the volumes of all packages.
34
- * quantity: Individual Quantity of a specific item being shipped.
35
- * package_type: Individual Type of packaging used (e.g., pallets, cartons).
36
- * weight: Individual Weight of a specific package.
37
- * measures: Individual Dimensions or measurements of a package.
38
- * stackable: Indicates whether the shipment is stackable (True or False).
39
- * volume: Individual Volume of a specific package.
40
- * commodity: Type of goods or commodities being shipped.
41
- * company: Name of the email sending company, also the shipping company or carrier.
42
- * incoterms: Choose available options: EXW, FCA, FAS, FOB, CFR, CIF, CPT, CIP, DAP, DPU, DDP.
43
-
44
- For attributes with multiple values, such as measures, volume, weight, package_type, and quantity, provide each value separately in a JSON format.
45
-
46
- ### Input:
47
- ```{input_text}```
48
-
49
- ### Response:
50
- """
51
-
52
- # Generate the result
53
- result = pipe(
54
- f"{prompt}",
55
- do_sample=True,
56
- max_new_tokens=max_new_tokens,
57
- temperature=temperature,
58
- top_k=top_k,
59
- top_p=top_p,
60
- num_return_sequences=1,
61
- )
62
 
63
- # Return the generated text
64
- return result[0]['generated_text']
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- examples = [['''Your task is to extract the information corresponding to the provided labels from the below given email.###Instruction:*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.For attributes with multiple values, such as measures, volume, weight, package_type, and quantity, provide each value separately in a JSON format.###Input:dubaiDear 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;[email protected] | 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.###Response:''']]
 
68
 
69
- # Create Gradio inputs with dropdown options for model selection
70
- model_options = ["DataIntelligenceTeam/model-NER-Phi", "DataIntelligenceTeam/mistral_7B_NER"]
71
- #"DataIntelligenceTeam/mistral_7B_NER"]
72
 
73
- # Create Gradio inputs
74
- inputs = [
75
- gr.inputs.Textbox(label="Input Text"),
76
- gr.inputs.Number(label="Max New Tokens", default=2000),
77
- gr.inputs.Slider(label="Temperature", minimum=0.0, maximum=1.0, default=0.1, step=0.01),
78
- gr.inputs.Number(label="Top K", default=0),
79
- gr.inputs.Number(label="Top P", default=0),
80
- gr.inputs.Dropdown(label="Model", choices=model_options, default=model_options[0])
81
- ]
82
 
83
- # Create a Gradio interface
84
- iface = gr.Interface(
85
- fn=generate_output,
86
- inputs=inputs,
87
- outputs="text",
88
- #examples=examples,
89
- title="Information Extraction with Open-Source LLM",
90
- description="Generate Information Extraction with OpenLLM.",
91
- debug=True
92
- )
93
 
94
- # Launch the interface
 
 
 
 
 
95
  iface.launch()
 
5
  from transformers import pipeline
6
  auth_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
7
 
8
+ import gradio as gr
9
+ from unsloth import FastLanguageModel
 
 
 
 
 
 
 
 
10
 
11
+ instruction = """
12
+ From the given email, extract the following key values. The keys are explained below:
13
+ * pickup_location: Street address of the origin location of goods.
14
+ * pickup_cap: Postal code or ZIP code of the pickup location.
15
+ * pickup_port: Port of pickup, often used in international shipping.
16
+ * pickup_state: Only Country of pickup location.
17
+ * delivery_location: Street address of the destination location of goods.
18
+ * delivery_cap: Postal code or ZIP code of delivery location.
19
+ * delivery_port: Port of delivery, similar to pickup port.
20
+ * delivery_state: State or region of delivery location.
21
+ * total_quantity: Overall quantity of shipped items (e.g., pieces, boxes). Calculate the total_quantity by summing the quantity of all packages.
22
+ * total_weight: Total weight of the shipment (e.g., kg, lbs). Calculate the total_weight by summing the weights of all packages.
23
+ * total_volume: Total volume of the shipment (e.g., cubic meters, cubic feet). Calculate the total_volume by summing the volumes of all packages.
24
+ * quantity: Individual Quantity of a specific item being shipped.
25
+ * package_type: Individual Type of packaging used (e.g., pallets, cartons).
26
+ * weight: Individual Weight of a specific package.
27
+ * measures: Individual Dimensions or measurements of a package.
28
+ * stackable: Indicates whether the shipment is stackable (True or False).
29
+ * volume: Individual Volume of a specific package.
30
+ * commodity: Type of goods or commodities being shipped.
31
+ * company: Name of the email sending company, also the shipping company or carrier.
32
+ * incoterms: Choose available options: EXW, FCA, FAS, FOB, CFR, CIF, CPT, CIP, DAP, DPU, DDP.
33
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
 
35
 
36
+ # Define the function for generating output based on input
37
+ def generate_output(input_text):
38
+ # Prompt for the instruction
39
+
40
+ output = ""
41
+ # Initialize the FastLanguageModel
42
+ model, tokenizer = FastLanguageModel.from_pretrained(
43
+ model_name = "sxandie/llama_3_8b_4bitQ",
44
+ max_seq_length = 2048,
45
+ dtype = None,
46
+ load_in_4bit = True,
47
+ )
48
+ FastLanguageModel.for_inference(model) # Enable native 2x faster inference
49
 
50
+ alpaca_prompt = f"""
51
+ Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
52
 
53
+ ### Instruction:
54
+ {instruction}
 
55
 
56
+ ### Input:
57
+ {input_text}
 
 
 
 
 
 
 
58
 
59
+ ### Response:
60
+ """
61
+ # Tokenize the input text
62
+ inputs = tokenizer([alpaca_prompt], return_tensors="pt").to("cuda")
63
+ # Generate outputs
64
+ outputs = model.generate(**inputs, max_new_tokens=2048, use_cache=True)
65
+ output = tokenizer.batch_decode(outputs)
66
+ return output
 
 
67
 
68
+ # Create Gradio interface
69
+ iface = gr.Interface(fn=generate_output,
70
+ inputs="text",
71
+ outputs="text",
72
+ title="Email Information Extraction",
73
+ description="Extract key information from the provided email.")
74
  iface.launch()