shivanikerai commited on
Commit
6f2a620
·
verified ·
1 Parent(s): 55a8e12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -2,10 +2,12 @@ import gradio as gr
2
 
3
  # gr.load("models/shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0").launch()
4
  # Load model directly
5
- from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
- tokenizer = AutoTokenizer.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0")
8
- model = AutoModelForCausalLM.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0")
 
 
9
  def generate_title_suggestions(keywords, product_info):
10
  # Define the roles and markers
11
  B_SYS, E_SYS = "<<SYS>>", "<</SYS>>"
@@ -17,26 +19,28 @@ def generate_title_suggestions(keywords, product_info):
17
  prompt = f"""{B_INST} {B_SYS} You are a helpful, respectful and honest assistant for ecommerce product title creation. {E_SYS}\nCreate a SEO optimized e-commerce product title for the keywords:{keywords.strip()}\n{B_in}{product_info}{E_in}\n{E_INST}\n\n{B_out}"""
18
 
19
 
20
- print("Prompt:")
21
- print(prompt)
22
-
23
- encoding = tokenizer(prompt, return_tensors="pt").to("cuda:0")
24
- output = model.generate(input_ids=encoding.input_ids,
25
- attention_mask=encoding.attention_mask,
26
- max_new_tokens=1024,
27
- do_sample=True,
28
- temperature=0.01,
29
- eos_token_id=tokenizer.eos_token_id,
30
- top_k=0)
 
 
31
 
32
- print()
33
 
34
  # Subtract the length of input_ids from output to get only the model's response
35
- output_text = tokenizer.decode(output[0, len(encoding.input_ids[0]):], skip_special_tokens=False)
36
- output_text = re.sub('\n+', '\n', output_text) # remove excessive newline characters
37
 
38
- print("Generated Assistant Response:")
39
- print(output_text)
40
  gr.Interface(
41
  generate_title_suggestions,
42
  inputs='text',
 
2
 
3
  # gr.load("models/shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0").launch()
4
  # Load model directly
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
6
 
7
+ pipe = pipeline("text-generation", model="shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0")
8
+
9
+ # tokenizer = AutoTokenizer.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0")
10
+ # model = AutoModelForCausalLM.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0")
11
  def generate_title_suggestions(keywords, product_info):
12
  # Define the roles and markers
13
  B_SYS, E_SYS = "<<SYS>>", "<</SYS>>"
 
19
  prompt = f"""{B_INST} {B_SYS} You are a helpful, respectful and honest assistant for ecommerce product title creation. {E_SYS}\nCreate a SEO optimized e-commerce product title for the keywords:{keywords.strip()}\n{B_in}{product_info}{E_in}\n{E_INST}\n\n{B_out}"""
20
 
21
 
22
+ # print("Prompt:")
23
+ # print(prompt)
24
+ predictions = pipeline(prompt)
25
+ output=((predictions[0]['generated_text']).split(B_out)[-1]).strip()
26
+ return (output)
27
+ # encoding = tokenizer(prompt, return_tensors="pt").to("cuda:0")
28
+ # output = model.generate(input_ids=encoding.input_ids,
29
+ # attention_mask=encoding.attention_mask,
30
+ # max_new_tokens=1024,
31
+ # do_sample=True,
32
+ # temperature=0.01,
33
+ # eos_token_id=tokenizer.eos_token_id,
34
+ # top_k=0)
35
 
36
+ # print()
37
 
38
  # Subtract the length of input_ids from output to get only the model's response
39
+ # output_text = tokenizer.decode(output[0, len(encoding.input_ids[0]):], skip_special_tokens=False)
40
+ # output_text = re.sub('\n+', '\n', output_text) # remove excessive newline characters
41
 
42
+ # print("Generated Assistant Response:")
43
+ # print(output_text)
44
  gr.Interface(
45
  generate_title_suggestions,
46
  inputs='text',