Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 9 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 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 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
#"DataIntelligenceTeam/mistral_7B_NER"]
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
inputs=
|
| 87 |
-
outputs
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
debug=True
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|