Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,117 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import torch
|
3 |
-
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
-
import base64
|
5 |
-
from datetime import datetime
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
party_dict = {
|
10 |
-
'AfD':'TweetGPT/
|
11 |
-
'Die Grünen':'TweetGPT/
|
12 |
-
'CDU/CSU':'TweetGPT/
|
13 |
-
'die Linke':'TweetGPT/
|
14 |
-
}
|
15 |
-
|
16 |
-
picture_paths = {
|
17 |
-
'blue_check': "TweetGPT/app_pictures/Twitter_Verified_Badge.svg.png",
|
18 |
-
'AfD' : "TweetGPT/app_pictures/app_pictures/afd_logo.png",
|
19 |
-
'CDU/CSU' : "TweetGPT/app_pictures/app_pictures/cdu_logo.png",
|
20 |
-
'Die Grünen' : "TweetGPT/app_pictures/app_pictures/gruene_logo.png",
|
21 |
-
'die Linke' : "TweetGPT/app_pictures/app_pictures/linke_logo.png"
|
22 |
-
}
|
23 |
-
|
24 |
-
party_info = {
|
25 |
-
"AfD": ("AfD", "@AfD"),
|
26 |
-
"CDU/CSU": ("CDU", "@CDU"),
|
27 |
-
"Die Grünen": ("Die Grünen", "@Die_Gruenen"),
|
28 |
-
"die Linke": ("Die Linke", "@dieLinke")
|
29 |
-
}
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
def generate_tweet(party, prompt):
|
34 |
-
device = torch.device("cpu")
|
35 |
-
model_path = party_dict[party]
|
36 |
-
tokenizer = GPT2Tokenizer.from_pretrained(model_path)
|
37 |
-
model = GPT2LMHeadModel.from_pretrained(model_path)
|
38 |
-
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
39 |
-
model.to(device)
|
40 |
-
|
41 |
-
sample_outputs = model.generate(
|
42 |
-
input_ids,
|
43 |
-
do_sample=True,
|
44 |
-
top_k=20,
|
45 |
-
max_length=280,
|
46 |
-
top_p=0.95,
|
47 |
-
num_return_sequences=1,
|
48 |
-
temperature=0.95,
|
49 |
-
pad_token_id=tokenizer.eos_token_id
|
50 |
-
)
|
51 |
-
|
52 |
-
generated_tweet = tokenizer.decode(sample_outputs[0], skip_special_tokens=True)
|
53 |
-
return generated_tweet
|
54 |
-
|
55 |
-
def get_base64_image(image_path):
|
56 |
-
with open(image_path, "rb") as image_file:
|
57 |
-
encoded_string = base64.b64encode(image_file.read())
|
58 |
-
return encoded_string.decode('utf-8')
|
59 |
-
|
60 |
-
def main():
|
61 |
-
st.title("Tweet GPT")
|
62 |
-
st.sidebar.header("**Analyzing Rhetorical Styles of German Political Parties**")
|
63 |
-
st.sidebar.write("This project analyzes rhetorical differences among major German parties by generating tweets tailored to their communication styles. Our motivation was to understand the nuances in language and messaging strategies, particularly the effective use of social media by right-wing parties to disseminate messages. We aimed to raise awareness of this issue and contribute to a better understanding of varying rhetorical approaches.")
|
64 |
-
st.sidebar.markdown("#### Motivation")
|
65 |
-
st.sidebar.markdown("- Understand language and messaging nuances")
|
66 |
-
st.sidebar.markdown("- Explore rhetorical differences and communication styles")
|
67 |
-
st.sidebar.markdown("- Provide insights into crafting messages for supporters")
|
68 |
-
st.sidebar.markdown("- Raise awareness of right-wing parties' social media use")
|
69 |
-
st.sidebar.write("Reference: https://www.zdf.de/nachrichten/politik/deutschland/afd-tiktok-erfolg-strategie-jugendliche-100.html")
|
70 |
-
|
71 |
-
tweet = ""
|
72 |
-
prompt = ""
|
73 |
-
|
74 |
-
col1, col2, col3 = st.columns([14, 1, 14])
|
75 |
-
|
76 |
-
with col1:
|
77 |
-
st.write("### Generate Tweets!")
|
78 |
-
party = st.selectbox("Party", ["AfD", "CDU/CSU", "Die Grünen", "die Linke"], key='party')
|
79 |
-
if st.button("Generate Tweet", key='generate', help="Click here to generate the tweet."):
|
80 |
-
prompt = st.session_state.get("prompt", "")
|
81 |
-
tweet = generate_tweet(party, prompt)
|
82 |
-
|
83 |
-
with col2:
|
84 |
-
st.write("")
|
85 |
-
|
86 |
-
with col3:
|
87 |
-
st.write("### Topic")
|
88 |
-
prompt = st.text_area("Tweet Keyword", key="prompt")
|
89 |
-
|
90 |
-
with st.container():
|
91 |
-
current_date = datetime.now().strftime("%B %d, %Y")
|
92 |
-
blue_check_base64 = get_base64_image(picture_paths['blue_check'])
|
93 |
-
party_logo_base64 = get_base64_image(picture_paths[party])
|
94 |
-
party, username = party_info[party]
|
95 |
-
|
96 |
-
if tweet:
|
97 |
-
tweet_display = f'''
|
98 |
-
<div style="background-color: white; padding: 10px; font-family: Helvetica Neue, sans-serif; border: 1px solid #ccc; color: black; border-radius: 10px;">
|
99 |
-
<div style="display: flex; align-items: center;">
|
100 |
-
<img src="data:image/png;base64,{party_logo_base64}" alt="{party} Logo" style="width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;">
|
101 |
-
<div style="display: flex; align-items: center;">
|
102 |
-
<div style="display: flex; align-items: center; margin-right: 5px;">
|
103 |
-
<p style="font-weight: bold; color: black;">{party}<img src="data:image/png;base64,{blue_check_base64}" alt="checkmark" style="width: 20px; height: 20px; vertical-align: middle; margin-left: 2px; margin-right: 2px;"><span style="font-weight: normal; color: gray;"> {username}</span> <span style="font-weight: normal; color: gray;">{current_date}</span></p>
|
104 |
-
</div>
|
105 |
-
</div>
|
106 |
-
</div>
|
107 |
-
<p style="color: black;">{tweet}</p>
|
108 |
-
</div>
|
109 |
-
'''
|
110 |
-
st.markdown(tweet_display, unsafe_allow_html=True)
|
111 |
-
|
112 |
-
st.write("## Our Insights")
|
113 |
-
st.write("We developed a tweet generator that analyzes the dataset for each party. To view the insights, select the button to see the most common topics each party wrote about.")
|
114 |
-
|
115 |
-
|
116 |
-
if __name__ == "__main__":
|
117 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
+
import base64
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
party_dict = {
|
10 |
+
'AfD':'TweetGPT/afd_gpt2',
|
11 |
+
'Die Grünen':'TweetGPT/gruene_gpt2',
|
12 |
+
'CDU/CSU':'TweetGPT/cdu_gpt2',
|
13 |
+
'die Linke':'TweetGPT/linke_gpt2',
|
14 |
+
}
|
15 |
+
|
16 |
+
picture_paths = {
|
17 |
+
'blue_check': "TweetGPT/app_pictures/Twitter_Verified_Badge.svg.png",
|
18 |
+
'AfD' : "TweetGPT/app_pictures/app_pictures/afd_logo.png",
|
19 |
+
'CDU/CSU' : "TweetGPT/app_pictures/app_pictures/cdu_logo.png",
|
20 |
+
'Die Grünen' : "TweetGPT/app_pictures/app_pictures/gruene_logo.png",
|
21 |
+
'die Linke' : "TweetGPT/app_pictures/app_pictures/linke_logo.png"
|
22 |
+
}
|
23 |
+
|
24 |
+
party_info = {
|
25 |
+
"AfD": ("AfD", "@AfD"),
|
26 |
+
"CDU/CSU": ("CDU", "@CDU"),
|
27 |
+
"Die Grünen": ("Die Grünen", "@Die_Gruenen"),
|
28 |
+
"die Linke": ("Die Linke", "@dieLinke")
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
def generate_tweet(party, prompt):
|
34 |
+
device = torch.device("cpu")
|
35 |
+
model_path = party_dict[party]
|
36 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_path)
|
37 |
+
model = GPT2LMHeadModel.from_pretrained(model_path)
|
38 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
39 |
+
model.to(device)
|
40 |
+
|
41 |
+
sample_outputs = model.generate(
|
42 |
+
input_ids,
|
43 |
+
do_sample=True,
|
44 |
+
top_k=20,
|
45 |
+
max_length=280,
|
46 |
+
top_p=0.95,
|
47 |
+
num_return_sequences=1,
|
48 |
+
temperature=0.95,
|
49 |
+
pad_token_id=tokenizer.eos_token_id
|
50 |
+
)
|
51 |
+
|
52 |
+
generated_tweet = tokenizer.decode(sample_outputs[0], skip_special_tokens=True)
|
53 |
+
return generated_tweet
|
54 |
+
|
55 |
+
def get_base64_image(image_path):
|
56 |
+
with open(image_path, "rb") as image_file:
|
57 |
+
encoded_string = base64.b64encode(image_file.read())
|
58 |
+
return encoded_string.decode('utf-8')
|
59 |
+
|
60 |
+
def main():
|
61 |
+
st.title("Tweet GPT")
|
62 |
+
st.sidebar.header("**Analyzing Rhetorical Styles of German Political Parties**")
|
63 |
+
st.sidebar.write("This project analyzes rhetorical differences among major German parties by generating tweets tailored to their communication styles. Our motivation was to understand the nuances in language and messaging strategies, particularly the effective use of social media by right-wing parties to disseminate messages. We aimed to raise awareness of this issue and contribute to a better understanding of varying rhetorical approaches.")
|
64 |
+
st.sidebar.markdown("#### Motivation")
|
65 |
+
st.sidebar.markdown("- Understand language and messaging nuances")
|
66 |
+
st.sidebar.markdown("- Explore rhetorical differences and communication styles")
|
67 |
+
st.sidebar.markdown("- Provide insights into crafting messages for supporters")
|
68 |
+
st.sidebar.markdown("- Raise awareness of right-wing parties' social media use")
|
69 |
+
st.sidebar.write("Reference: https://www.zdf.de/nachrichten/politik/deutschland/afd-tiktok-erfolg-strategie-jugendliche-100.html")
|
70 |
+
|
71 |
+
tweet = ""
|
72 |
+
prompt = ""
|
73 |
+
|
74 |
+
col1, col2, col3 = st.columns([14, 1, 14])
|
75 |
+
|
76 |
+
with col1:
|
77 |
+
st.write("### Generate Tweets!")
|
78 |
+
party = st.selectbox("Party", ["AfD", "CDU/CSU", "Die Grünen", "die Linke"], key='party')
|
79 |
+
if st.button("Generate Tweet", key='generate', help="Click here to generate the tweet."):
|
80 |
+
prompt = st.session_state.get("prompt", "")
|
81 |
+
tweet = generate_tweet(party, prompt)
|
82 |
+
|
83 |
+
with col2:
|
84 |
+
st.write("")
|
85 |
+
|
86 |
+
with col3:
|
87 |
+
st.write("### Topic")
|
88 |
+
prompt = st.text_area("Tweet Keyword", key="prompt")
|
89 |
+
|
90 |
+
with st.container():
|
91 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
92 |
+
blue_check_base64 = get_base64_image(picture_paths['blue_check'])
|
93 |
+
party_logo_base64 = get_base64_image(picture_paths[party])
|
94 |
+
party, username = party_info[party]
|
95 |
+
|
96 |
+
if tweet:
|
97 |
+
tweet_display = f'''
|
98 |
+
<div style="background-color: white; padding: 10px; font-family: Helvetica Neue, sans-serif; border: 1px solid #ccc; color: black; border-radius: 10px;">
|
99 |
+
<div style="display: flex; align-items: center;">
|
100 |
+
<img src="data:image/png;base64,{party_logo_base64}" alt="{party} Logo" style="width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;">
|
101 |
+
<div style="display: flex; align-items: center;">
|
102 |
+
<div style="display: flex; align-items: center; margin-right: 5px;">
|
103 |
+
<p style="font-weight: bold; color: black;">{party}<img src="data:image/png;base64,{blue_check_base64}" alt="checkmark" style="width: 20px; height: 20px; vertical-align: middle; margin-left: 2px; margin-right: 2px;"><span style="font-weight: normal; color: gray;"> {username}</span> <span style="font-weight: normal; color: gray;">{current_date}</span></p>
|
104 |
+
</div>
|
105 |
+
</div>
|
106 |
+
</div>
|
107 |
+
<p style="color: black;">{tweet}</p>
|
108 |
+
</div>
|
109 |
+
'''
|
110 |
+
st.markdown(tweet_display, unsafe_allow_html=True)
|
111 |
+
|
112 |
+
st.write("## Our Insights")
|
113 |
+
st.write("We developed a tweet generator that analyzes the dataset for each party. To view the insights, select the button to see the most common topics each party wrote about.")
|
114 |
+
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
main()
|