shivanikerai commited on
Commit
04d2512
·
verified ·
1 Parent(s): 78a8a9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -1
app.py CHANGED
@@ -1,3 +1,45 @@
1
  import gradio as gr
2
 
3
- gr.load("models/shivanikerai/TinyLlama-1.1B-Chat-v1.0-seo-optimised-title-suggestion-v1.0").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  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>>"
12
+ B_INST, E_INST = "[INST]", "[/INST]"
13
+ B_in, E_in = "[Product Details]", "[/Product Details]"
14
+ B_out, E_out = "[Suggested Titles]", "[/Suggested Titles]"
15
+
16
+ # Format your prompt template
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',
43
+ outputs='text',
44
+ title="Title Suggestion",
45
+ ).launch()