Spaces:
Sleeping
Sleeping
Hugo
commited on
Commit
·
c04572a
1
Parent(s):
a2d5751
code added
Browse files- app.py +72 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import os
|
4 |
+
import os.path
|
5 |
+
import pickle
|
6 |
+
import torch
|
7 |
+
|
8 |
+
model_id = "HiGenius/Headline-Generation-Model"
|
9 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
10 |
+
|
11 |
+
@st.cache_resource
|
12 |
+
def load_model():
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
15 |
+
tokenizer.pad_token = tokenizer.eos_token
|
16 |
+
tokenizer.padding_side='left'
|
17 |
+
|
18 |
+
return tokenizer, model
|
19 |
+
|
20 |
+
tokenizer, model = load_model()
|
21 |
+
|
22 |
+
guideline_path = "./llama_3.2_3b_epoch2_iteration50.pkl"
|
23 |
+
guideline_results = pickle.load(open(guideline_path,'rb'))
|
24 |
+
guidelines = guideline_results['best_rational']
|
25 |
+
|
26 |
+
def process_prompt(tokenizer, content, video_summary = '', guidelines = None):
|
27 |
+
if guidelines:
|
28 |
+
system_prompt = "You are a helpful assistant that writes engaging headlines. To maximize engagement, you may follow these proven guidelines:\n" + guidelines
|
29 |
+
else:
|
30 |
+
system_prompt = "You are a helpful assistant that writes engaging headlines."
|
31 |
+
|
32 |
+
user_prompt = (
|
33 |
+
f"Below is an article and its accompanying video summary:\n\n"
|
34 |
+
f"Article Content:\n{content}\n\n"
|
35 |
+
f"Video Summary:\n{'None' if video_summary == '' else video_summary}\n\n"
|
36 |
+
f"Write ONLY a single engaging headline that accurately reflects the article. Do not include any additional text, explanations, or options."
|
37 |
+
)
|
38 |
+
messages = [
|
39 |
+
{"role": "system", "content": system_prompt},
|
40 |
+
{"role": "user", "content": user_prompt},
|
41 |
+
]
|
42 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
43 |
+
return prompt
|
44 |
+
|
45 |
+
|
46 |
+
st.title("Article Headline Writer")
|
47 |
+
st.write("Write a catchy headline from content and video summary.")
|
48 |
+
|
49 |
+
# Inputs for content and video summary
|
50 |
+
content = st.text_area("Enter the article content:", placeholder="Type the main content of the article here...")
|
51 |
+
video_summary = st.text_area("Enter the summary of the article's accompanying video (optional):", placeholder="Type the summary of the video related to the article...")
|
52 |
+
|
53 |
+
if st.button("Generate Headline"):
|
54 |
+
if content.strip():
|
55 |
+
if not video_summary.strip():
|
56 |
+
video_summary = ''
|
57 |
+
prompt = process_prompt(tokenizer, content, video_summary, guidelines)
|
58 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
59 |
+
|
60 |
+
st.write("### Generated 5 Potential Headlines:")
|
61 |
+
for i in range(5):
|
62 |
+
st.write(f"### Headline {i+1}")
|
63 |
+
outputs = model.generate(**inputs,
|
64 |
+
max_new_tokens=60,
|
65 |
+
num_return_sequences=1,
|
66 |
+
do_sample=True,
|
67 |
+
temperature=0.7)
|
68 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
69 |
+
response = response.replace('"', '')
|
70 |
+
st.write(f"{response}")
|
71 |
+
else:
|
72 |
+
st.write("Please enter a valid prompt.")
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|