Spaces:
Running
Running
File size: 16,101 Bytes
0d5285b 8f94101 0d5285b 8dbecdf 8f94101 0d5285b 33d9843 0d5285b 9ebd034 a1e299f a04917c a1e299f 9ebd034 a04917c 0d5285b 33d9843 0d5285b 8f94101 0d5285b 9ebd034 0d5285b 8f94101 33d9843 8f94101 33d9843 8f94101 0d5285b 8f94101 0d5285b 8f94101 0d5285b 8f94101 0d5285b 9ebd034 0d5285b 8f94101 0d5285b 8f94101 33d9843 8f94101 0d5285b 8f94101 33d9843 8f94101 0d5285b ff6730f 8dbecdf 0d5285b 8f94101 33d9843 0d5285b 8f94101 0d5285b 8f94101 0d5285b 4373b72 0d5285b 635bb4c 8f94101 0d5285b 9ebd034 0d5285b 8f94101 0d5285b 8f94101 9ebd034 8f94101 0d5285b 8f94101 33d9843 8f94101 33d9843 8f94101 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
import streamlit as st
from dotenv import load_dotenv
import requests
from bs4 import BeautifulSoup
from bs4 import NavigableString
import openai
import os
import hmac
from st_copy_to_clipboard import st_copy_to_clipboard
load_dotenv()
def create_article(length_option, articles, params, web_page_option):
article_string = "; ".join(f"Artikel {index + 1}: {artikel}" for index, artikel in enumerate(articles))
if length_option == "Kurz":
length = 8
elif length_option == "Mittel":
length = 14
elif length_option == "Lang":
length = 20
openai.api_key = os.environ.get("OPEN_API_KEY")
openai.api_base = os.environ.get("OPEN_API_BASE")
openai.api_type = os.environ.get("OPEN_API_TYPE")
openai.api_version = os.environ.get("OPEN_API_VERSION")
if web_page_option == "heute.at":
writing_style = os.environ.get("WRITING_STYLE_HEUTE")
else:
writing_style = os.environ.get("WRITING_STYLE_GESUND")
try:
res = openai.ChatCompletion.create(
engine="gpt-4-1106",
temperature=0.4,
messages=[
{
"role": "system",
"content": f"You are a professional journalist whose task is to write your own article based on one or more articles. This article should combine the content of the original articles, but have its own writing style, which is as follows: {writing_style} Do not use unusual phrases or neologisms from the original articles. The length of your article should be {length} sentences long.",
},
{
"role": "system",
"content": f"Source articles: {article_string}"
},
{
"role": "system",
"content": f"Please also note the following instructions defined by the user: {params}"
},
{
"role": "system",
"content": "Schreibe den Artikel immer in deutscher Sprache."
}
],
)
return res["choices"][0]["message"]["content"]
except Exception as e:
print(f"Fehler beim erstellen des artikels: {str(e)}")
st.error(f"Something went wrong: {str(e)}", icon="🚨")
def create_headline(article, web_page_option):
openai.api_key = os.environ.get("OPEN_API_KEY")
openai.api_base = os.environ.get("OPEN_API_BASE")
openai.api_type = os.environ.get("OPEN_API_TYPE")
openai.api_version = os.environ.get("OPEN_API_VERSION")
if web_page_option == "heute.at":
writing_style = os.environ.get("WRITING_STYLE_HEUTE")
else:
writing_style = os.environ.get("WRITING_STYLE_GESUND")
try:
res = openai.ChatCompletion.create(
engine="gpt-4-1106",
temperature=0.4,
messages=[
{
"role": "system",
"content": f"You are a professional journalist and have the task of generating a headline for an article you have written. I will give you the writing style that was used to create the article as info. Writing style: {writing_style} The headline should be as short as possible, but still capture the essence of the article. It should be a maximum of 10 words long",
},
{
"role": "system",
"content": f"Source article: {article}"
},
{
"role": "system",
"content": "Schreibe die Headline immer in deutscher Sprache."
}
],
)
return res["choices"][0]["message"]["content"]
except Exception as e:
print(f"Fehler beim erstellen der headline: {str(e)}")
st.error(f"Something went wrong: {str(e)}", icon="🚨")
def extract_text_from_element(element):
# Initialisiere einen leeren Textstring
text_content = ""
# Überprüfe, ob das Element ein <p>, <ul> oder <ol>-Tag ist
if element.name in ['p', 'ul', 'ol']:
# Extrahiere den Text des Tags und füge ihn zum Textstring hinzu
text_content += element.get_text() + '\n'
# Überprüfe, ob das Element ein Tag mit Kindern ist (kein Textknoten)
if not isinstance(element, NavigableString):
# Rekursiv durch jedes Child-Element gehen und den Text hinzufügen
for child in element.children:
text_content += extract_text_from_element(child)
return text_content
def extract_article(url):
# Webseite herunterladen
response = requests.get(url)
# Überprüfen, ob die Anfrage erfolgreich war (Status-Code 200)
if response.status_code == 200:
# HTML-Inhalt parsen
soup = BeautifulSoup(response.text, 'html.parser')
# Finden Sie das <article>-Tag (nehmen Sie an, dass es eins gibt)
article_tag = soup.find('article')
if article_tag:
# Starte die Rekursion für jedes Child-Element des <article>-Tags
extracted_text = extract_text_from_element(article_tag)
stripped_text = filter_empty_lines(extracted_text)
return stripped_text
else:
print("Kein <article>-Tag gefunden.")
return None
else:
# Falls die Anfrage nicht erfolgreich war, eine Fehlermeldung ausgeben
print(f"Fehler: {response.status_code}")
return None
def filter_empty_lines(text):
# Teile den Text in Zeilen auf
lines = text.split('\n')
# Filtere leere Zeilen heraus
non_empty_lines = filter(lambda line: line.strip() != '', lines)
# Verbinde die nicht leeren Zeilen zu einem String
filtered_text = '\n'.join(non_empty_lines)
return filtered_text
def extract_article_links(**kwargs):
# print(len(kwargs["links"]))
with st.spinner("Extrahiere..."):
results = []
for link in kwargs["links"]:
results.append(extract_article(link))
st.session_state["extracted_articles"] = results
if st.session_state["process_step"] < 1:
st.session_state["process_step"] += 1
st.session_state["selected_page"] = 1
def extract_article_links_for_heading(**kwargs):
article = extract_article(kwargs["link"])
def finalize_articles():
final_articles = []
for i in range(len(st.session_state["extracted_articles"])):
final_articles.append(st.session_state["final_article_"+str(i+1)])
st.session_state["final_articles"] = final_articles
if st.session_state["process_step"] < 2:
st.session_state["process_step"] += 1
st.session_state["selected_page"] += 1
def increase_page():
if st.session_state["selected_page"] <= st.session_state["process_step"]:
st.session_state["selected_page"] += 1
def decrease_page():
if st.session_state["selected_page"] > 0:
st.session_state["selected_page"] -= 1
def on_click_handler_generate_article(**kwargs):
with st.spinner("Generiere Artikel..."):
created_article = create_article(kwargs["length_option"],kwargs["final_articles"],kwargs["add_info"], kwargs["webpage_option"])
headline = create_headline(created_article, kwargs["webpage_option"])
st.session_state["generated_article"] = created_article
st.session_state["generated_headline"] = headline
if st.session_state["process_step"] < 3:
st.session_state["process_step"] += 1
st.session_state["selected_page"] += 1
def reset_session_state():
st.session_state["extracted_articles"] = []
st.session_state["article_links"] = []
st.session_state["final_articles"] = []
st.session_state["process_step"] = 0
st.session_state["selected_page"] = 0
st.session_state["generated_article"] = ""
if "extracted_articles" not in st.session_state:
st.session_state["extracted_articles"] = []
if "article_links" not in st.session_state:
st.session_state["article_links"] = []
if "final_articles" not in st.session_state:
st.session_state["final_articles"] = []
if "process_step" not in st.session_state:
st.session_state["process_step"] = 0
if "selected_page" not in st.session_state:
st.session_state["selected_page"] = 0
if "generated_article" not in st.session_state:
st.session_state["generated_article"] = ""
if "function_state" not in st.session_state:
st.session_state["function_state"] = True
if "generated_headline" not in st.session_state:
st.session_state["generated_headline"] = ""
if "webpage_option" not in st.session_state:
st.session_state["webpage_option"] = "heute.at"
PROCESS_STEPS = ["Artikel Extraktion","Artikel Finalisierung","Artikel Generierung","Artikel Ausgabe"]
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], os.environ.get("PASSWORD")):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the password is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
if not check_password():
st.stop() # Do not continue if check_password is not True.
col1, col2 = st.columns([2, 1])
col1.title("TINA")
if st.session_state["webpage_option"] == "heute.at":
col2.image("heute_logo.png")
else:
col2.image("gesundheitstrends_logo.png")
st.radio("Wähle die Webseite aus für die Du einen Artikel generieren möchtest",["heute.at","gesundheitstrends.com"], key="webpage_option")
# with st.sidebar:
# st.title("Funktions Auswahl")
# st.write("Hier kannst Du zwischen der Artikel generierung und Überschrift generierung wählen.")
# st.button("Artikel Generierung", key="article_gen_btn", use_container_width=True, on_click=lambda: st.session_state.update({"function_state": True}))
# st.button("Überschrift Generierung", key="headline_gen_btn", use_container_width=True, on_click=lambda: st.session_state.update({"function_state": False}))
if st.session_state["function_state"]:
tab_col1, tab_col2, tab_col3, tab_col4 = st.columns([1, 1, 1, 1])
tab_col1.button("Artikel Extraktion", key="tab1", use_container_width=True, on_click=lambda: st.session_state.update({"selected_page": 0}), disabled=st.session_state["selected_page"] == 0)
tab_col2.button("Artikel Finalisierung", key="tab2", use_container_width=True, on_click=lambda: st.session_state.update({"selected_page": 1}), disabled=st.session_state["process_step"] < 1 or st.session_state["selected_page"] == 1)
tab_col3.button("Artikel Generierung", key="tab3", use_container_width=True, on_click=lambda: st.session_state.update({"selected_page": 2}), disabled=st.session_state["process_step"] < 2 or st.session_state["selected_page"] == 2)
tab_col4.button("Artikel Ausgabe", key="tab4", use_container_width=True, on_click=lambda: st.session_state.update({"selected_page": 3}), disabled=st.session_state["process_step"] < 3 or st.session_state["selected_page"] == 3)
nav_col1, nav_col2, nav_col3 = st.columns([1, 4, 1])
nav_col1.button("◀️", key="nav1", use_container_width=True, on_click=decrease_page, disabled=st.session_state["selected_page"] == 0)
nav_col2.markdown(f"<div style='text-align: center;'>{PROCESS_STEPS[st.session_state['selected_page']]}</div>", unsafe_allow_html=True)
nav_col3.button("▶️", key="nav2", use_container_width=True, on_click=increase_page, disabled=st.session_state["selected_page"] == st.session_state["process_step"])
if(st.session_state["selected_page"] == 0):
st.write("Bitte gebe die Links der Artikel ein, welche Du extrahiert haben möchtest.")
st.text_input("Gebe den "+str(len(st.session_state["article_links"])+1)+". Link ein:",key="link_input_"+str(len(st.session_state["article_links"])+1))
if st.session_state["link_input_"+str(len(st.session_state["article_links"])+1)]:
st.session_state["article_links"].append(st.session_state["link_input_"+str(len(st.session_state["article_links"])+1)])
st.rerun()
for i in range(len(st.session_state["article_links"])):
st.write(f"Link nr. {i+1}:\n\n{st.session_state['article_links'][i]}")
if(len(st.session_state["article_links"])>0):
try:
st.button("Extrahiere Artikel",on_click=extract_article_links,kwargs={"links":st.session_state["article_links"]})
except Exception as e:
print(f"Fehler beim extrahieren der artikel: {str(e)}")
st.error(f"Du hast einen oder mehrere Links nicht in dem korrekten Format angegeben. Bitte Lade die Seite neu und benutze korrekte Links: {str(e)}", icon="🚨")
elif(st.session_state["selected_page"] == 1):
st.write("Hier kannst Du die extrahierten Artikel ansehen und bei Bedarf anpassen.")
for i,article in enumerate(st.session_state["extracted_articles"]):
with st.expander(f"Artikel {i+1}"):
if article:
st.text_area("Editiere die Artikel, falls nötig:", value=article, key="final_article_"+str(i+1), height=500)
else:
st.info("Die Webseite des Artikels blockiert das automatische extrahieren von Artikeln. Wenn Du den Artikel dennoch benutzen möchtest, dann kannst Du diesen kopieren und einfügen.", icon="ℹ️")
st.text_area("Füge den Artikel ein, falls nötig:", value=article, key="final_article_"+str(i+1), height=500)
st.button("Artikel finalisieren",on_click=finalize_articles)
elif(st.session_state["selected_page"] == 2):
for i in range(len(st.session_state["final_articles"])):
if st.session_state["final_articles"][i]:
with st.expander("Artikel "+ str(i+1)):
st.write(st.session_state["final_articles"][i])
if len(st.session_state["final_articles"]) > 0:
st.write("Benutzte Artikel:")
for i,link in enumerate(st.session_state["article_links"]):
st.write(f"Link {i+1}: {link}")
st.text_area("Füge weitere Informationen für den Prompt hinzu, falls nötig:",key="add_info")
st.write("Artikellänge")
st.radio("Optionen",["Kurz", "Mittel", "Lang"], key="length_option")
st.button("Artikel generieren", key="article_btn", on_click=on_click_handler_generate_article, kwargs={"length_option":st.session_state["length_option"],"final_articles":st.session_state["final_articles"],"add_info":st.session_state["add_info"], "webpage_option":st.session_state["webpage_option"]})
elif(st.session_state["selected_page"] == 3):
st.write(f"**{st.session_state['generated_headline']}**")
st.write(st.session_state["generated_article"])
st.write("Kopieren Sie den Artikel: ")
st_copy_to_clipboard(st.session_state["generated_headline"]+"\n"+st.session_state["generated_article"])
st.button("Neuen Artikel generieren", key="reset_btn", on_click=reset_session_state)
else:
st.write("Bitte gebe den Link ein für welchen du eine Überschrift generiert haben möchtest.")
st.text_input("Gebe den Link ein:",key="headline_link_input")
if st.session_state["headline_link_input"]:
st.button("Generiere Überschrift",on_click=extract_article_links_for_heading,kwargs={"link":[st.session_state["headline_link_input"]]})
|