Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,23 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def convert_to_dictionary(input_string):
|
| 16 |
try:
|
|
@@ -117,9 +124,26 @@ def ner_title(title):
|
|
| 117 |
B_in, E_in = "[Title]", "[/Title]"
|
| 118 |
# Format your prompt template
|
| 119 |
prompt = f"""{B_INST} {B_SYS} You are a helpful assistant that provides accurate and concise responses. {E_SYS}\nExtract named entities from the given product title. Provide the output in JSON format.\n{B_in} {title.strip()} {E_in}\n{E_INST}\n\n### NER Response:\n{{"{title.split()[0].lower()}"""
|
| 120 |
-
output = query({
|
| 121 |
-
"inputs": prompt,
|
| 122 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
#return eval(pipe(text)[0]["generated_text"].split("### NER Response:\n")[-1])
|
| 125 |
return convert_to_dictionary((output[0]['generated_text'].split("### NER Response:\n")[-1]))
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import re
|
| 3 |
|
| 4 |
+
# Use a pipeline as a high-level helper
|
| 5 |
+
# Load model directly
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0")
|
| 10 |
+
# import requests
|
| 11 |
+
# import os
|
| 12 |
+
# token=os.environ.get("HUGGING_FACE_TOKEN")
|
| 13 |
|
| 14 |
+
# API_URL = "https://api-inference.huggingface.co/models/shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0"
|
| 15 |
+
# headers = {"Authorization": f"Bearer {token}"}
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# def query(payload):
|
| 19 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
| 20 |
+
# return response.json()
|
| 21 |
|
| 22 |
def convert_to_dictionary(input_string):
|
| 23 |
try:
|
|
|
|
| 124 |
B_in, E_in = "[Title]", "[/Title]"
|
| 125 |
# Format your prompt template
|
| 126 |
prompt = f"""{B_INST} {B_SYS} You are a helpful assistant that provides accurate and concise responses. {E_SYS}\nExtract named entities from the given product title. Provide the output in JSON format.\n{B_in} {title.strip()} {E_in}\n{E_INST}\n\n### NER Response:\n{{"{title.split()[0].lower()}"""
|
| 127 |
+
# output = query({
|
| 128 |
+
# "inputs": prompt,
|
| 129 |
+
# })
|
| 130 |
+
|
| 131 |
+
encoding = tokenizer(prompt, return_tensors="pt").to("cuda:0")
|
| 132 |
+
output = model.generate(input_ids=encoding.input_ids,
|
| 133 |
+
attention_mask=encoding.attention_mask,
|
| 134 |
+
max_new_tokens=512,
|
| 135 |
+
do_sample=True,
|
| 136 |
+
temperature=0.01,
|
| 137 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 138 |
+
top_k=0)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# Subtract the length of input_ids from output to get only the model's response
|
| 143 |
+
output_text = tokenizer.decode(output[0, len(encoding.input_ids[0]):], skip_special_tokens=False)
|
| 144 |
+
output = re.sub('\n+', '\n', output_text) # remove excessive newline characters
|
| 145 |
+
#output = f"""{{\"{title.split()[0].lower()} {output_text}"""
|
| 146 |
+
#output = re.sub(' ": "', '": "', output)
|
| 147 |
|
| 148 |
#return eval(pipe(text)[0]["generated_text"].split("### NER Response:\n")[-1])
|
| 149 |
return convert_to_dictionary((output[0]['generated_text'].split("### NER Response:\n")[-1]))
|