Spaces:
Runtime error
Runtime error
init keyword commit
Browse files- app.py +579 -0
- requirements.txt +1 -1
- tensora_logo.png +0 -0
app.py
CHANGED
@@ -7,3 +7,582 @@ import streamlit as st
|
|
7 |
from bs4 import BeautifulSoup, NavigableString
|
8 |
from dotenv import load_dotenv
|
9 |
from st_copy_to_clipboard import st_copy_to_clipboard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from bs4 import BeautifulSoup, NavigableString
|
8 |
from dotenv import load_dotenv
|
9 |
from st_copy_to_clipboard import st_copy_to_clipboard
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
|
14 |
+
def get_related_studies(article: str):
|
15 |
+
with st.spinner("Extrahiere Studien..."):
|
16 |
+
url = f'https://serpapi.com/search.json?engine=google_scholar&api_key={os.getenv("SERP_API_KEY")}&as_ylo=2018&q='
|
17 |
+
url += extract_scholar_query(article).replace('"', "")
|
18 |
+
try:
|
19 |
+
response = requests.get(url)
|
20 |
+
if response.status_code == 200:
|
21 |
+
data = response.json()
|
22 |
+
if data.get("organic_results"):
|
23 |
+
results = []
|
24 |
+
for result in data["organic_results"]:
|
25 |
+
results.append(
|
26 |
+
{
|
27 |
+
"title": result["title"],
|
28 |
+
"link": result["link"],
|
29 |
+
}
|
30 |
+
)
|
31 |
+
st.session_state["studie_links"] = results
|
32 |
+
else:
|
33 |
+
st.session_state["studie_links"] = []
|
34 |
+
else:
|
35 |
+
st.session_state["studie_links"] = []
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Fehler beim extrahieren der Studien: {str(e)}")
|
38 |
+
st.error(f"Something went wrong: {str(e)}", icon="🚨")
|
39 |
+
|
40 |
+
|
41 |
+
def extract_scholar_query(article: str):
|
42 |
+
openai.api_key = os.environ.get("OPEN_API_KEY")
|
43 |
+
openai.api_base = os.environ.get("OPEN_API_BASE")
|
44 |
+
openai.api_type = os.environ.get("OPEN_API_TYPE")
|
45 |
+
openai.api_version = os.environ.get("OPEN_API_VERSION")
|
46 |
+
try:
|
47 |
+
res = openai.ChatCompletion.create(
|
48 |
+
engine="gpt-4-1106",
|
49 |
+
temperature=0.2,
|
50 |
+
messages=[
|
51 |
+
{
|
52 |
+
"role": "system",
|
53 |
+
"content": f"You are a professional journalist whose task is to find related studies based on an article you have written. Please write a query that you would use to search for related studies on Google Scholar. Please make sure that the query is specific enough and cotains a maximum of 4 words. Only include one query in your output. Do not write multiple querys with an AND or OR. The article you have written is as follows: {article}.",
|
54 |
+
}
|
55 |
+
],
|
56 |
+
)
|
57 |
+
return res["choices"][0]["message"]["content"]
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Fehler beim extrahieren der Query: {str(e)}")
|
60 |
+
st.error(f"Something went wrong: {str(e)}", icon="🚨")
|
61 |
+
return ""
|
62 |
+
|
63 |
+
|
64 |
+
def create_article(length_option, articles, params, web_page_option):
|
65 |
+
|
66 |
+
if length_option == "Kurz":
|
67 |
+
length = os.environ.get("SHORT_LENGTH")
|
68 |
+
elif length_option == "Mittel":
|
69 |
+
length = os.environ.get("MEDIUM_LENGTH")
|
70 |
+
elif length_option == "Lang":
|
71 |
+
length = os.environ.get("LONG_LENGTH")
|
72 |
+
|
73 |
+
print(f"Length: {length}")
|
74 |
+
|
75 |
+
openai.api_key = os.environ.get("OPEN_API_KEY")
|
76 |
+
openai.api_base = os.environ.get("OPEN_API_BASE")
|
77 |
+
openai.api_type = os.environ.get("OPEN_API_TYPE")
|
78 |
+
openai.api_version = os.environ.get("OPEN_API_VERSION")
|
79 |
+
|
80 |
+
if web_page_option == "Boulevard":
|
81 |
+
writing_style = os.environ.get("WRITING_STYLE_HEUTE")
|
82 |
+
else:
|
83 |
+
writing_style = os.environ.get("WRITING_STYLE_GESUND")
|
84 |
+
|
85 |
+
try:
|
86 |
+
if len(articles) > 0:
|
87 |
+
article_string = "; ".join(
|
88 |
+
f"Artikel {index + 1}: {artikel}"
|
89 |
+
for index, artikel in enumerate(articles)
|
90 |
+
)
|
91 |
+
messages = [
|
92 |
+
{
|
93 |
+
"role": "system",
|
94 |
+
"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.",
|
95 |
+
},
|
96 |
+
{"role": "system", "content": f"Source articles: {article_string}"},
|
97 |
+
{
|
98 |
+
"role": "system",
|
99 |
+
"content": f"Please also note the following instructions defined by the user: {params}",
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"role": "system",
|
103 |
+
"content": "Schreibe den Artikel immer in deutscher Sprache.",
|
104 |
+
},
|
105 |
+
]
|
106 |
+
else:
|
107 |
+
messages = [
|
108 |
+
{
|
109 |
+
"role": "system",
|
110 |
+
"content": f"You are a professional journalist whose task is to write an article based on your own notes. This article should be written in the following writing style: {writing_style} The length of your article should be {length} sentences long.",
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"role": "system",
|
114 |
+
"content": f"Please write the article based on the following user input: {params}",
|
115 |
+
},
|
116 |
+
{
|
117 |
+
"role": "system",
|
118 |
+
"content": "Schreibe den Artikel immer in deutscher Sprache.",
|
119 |
+
},
|
120 |
+
]
|
121 |
+
res = openai.ChatCompletion.create(
|
122 |
+
engine="gpt-35-16k",
|
123 |
+
temperature=0.4,
|
124 |
+
max_tokens=12000,
|
125 |
+
messages=messages,
|
126 |
+
)
|
127 |
+
return res["choices"][0]["message"]["content"]
|
128 |
+
except Exception as e:
|
129 |
+
print(f"Fehler beim erstellen des artikels: {str(e)}")
|
130 |
+
st.error(f"Something went wrong: {str(e)}", icon="🚨")
|
131 |
+
|
132 |
+
|
133 |
+
def create_headline(article, web_page_option):
|
134 |
+
openai.api_key = os.environ.get("OPEN_API_KEY")
|
135 |
+
openai.api_base = os.environ.get("OPEN_API_BASE")
|
136 |
+
openai.api_type = os.environ.get("OPEN_API_TYPE")
|
137 |
+
openai.api_version = os.environ.get("OPEN_API_VERSION")
|
138 |
+
|
139 |
+
if web_page_option == "Boulevard":
|
140 |
+
writing_style = os.environ.get("WRITING_STYLE_HEUTE")
|
141 |
+
else:
|
142 |
+
writing_style = os.environ.get("WRITING_STYLE_GESUND")
|
143 |
+
|
144 |
+
try:
|
145 |
+
res = openai.ChatCompletion.create(
|
146 |
+
engine="gpt-4-1106",
|
147 |
+
temperature=0.4,
|
148 |
+
messages=[
|
149 |
+
{
|
150 |
+
"role": "system",
|
151 |
+
"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",
|
152 |
+
},
|
153 |
+
{"role": "system", "content": f"Source article: {article}"},
|
154 |
+
{
|
155 |
+
"role": "system",
|
156 |
+
"content": "Schreibe die Headline immer in deutscher Sprache.",
|
157 |
+
},
|
158 |
+
],
|
159 |
+
)
|
160 |
+
return res["choices"][0]["message"]["content"]
|
161 |
+
except Exception as e:
|
162 |
+
print(f"Fehler beim erstellen der headline: {str(e)}")
|
163 |
+
st.error(f"Something went wrong: {str(e)}", icon="🚨")
|
164 |
+
|
165 |
+
|
166 |
+
def extract_text_from_element(element):
|
167 |
+
# Initialisiere einen leeren Textstring
|
168 |
+
text_content = ""
|
169 |
+
|
170 |
+
# Überprüfe, ob das Element ein <p>, <ul> oder <ol>-Tag ist
|
171 |
+
if element.name in ["p", "ul", "ol"]:
|
172 |
+
# Extrahiere den Text des Tags und füge ihn zum Textstring hinzu
|
173 |
+
text_content += element.get_text() + "\n"
|
174 |
+
|
175 |
+
# Überprüfe, ob das Element ein Tag mit Kindern ist (kein Textknoten)
|
176 |
+
if not isinstance(element, NavigableString):
|
177 |
+
# Rekursiv durch jedes Child-Element gehen und den Text hinzufügen
|
178 |
+
for child in element.children:
|
179 |
+
text_content += extract_text_from_element(child)
|
180 |
+
|
181 |
+
return text_content
|
182 |
+
|
183 |
+
|
184 |
+
def extract_article(url):
|
185 |
+
# Webseite herunterladen
|
186 |
+
response = requests.get(url)
|
187 |
+
|
188 |
+
# Überprüfen, ob die Anfrage erfolgreich war (Status-Code 200)
|
189 |
+
if response.status_code == 200:
|
190 |
+
# HTML-Inhalt parsen
|
191 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
192 |
+
|
193 |
+
# Finden Sie das <article>-Tag (nehmen Sie an, dass es eins gibt)
|
194 |
+
article_tag = soup.find("article")
|
195 |
+
|
196 |
+
if article_tag:
|
197 |
+
# Starte die Rekursion für jedes Child-Element des <article>-Tags
|
198 |
+
extracted_text = extract_text_from_element(article_tag)
|
199 |
+
stripped_text = filter_empty_lines(extracted_text)
|
200 |
+
return stripped_text
|
201 |
+
else:
|
202 |
+
print("Kein <article>-Tag gefunden.")
|
203 |
+
return None
|
204 |
+
else:
|
205 |
+
# Falls die Anfrage nicht erfolgreich war, eine Fehlermeldung ausgeben
|
206 |
+
print(f"Fehler: {response.status_code}")
|
207 |
+
return None
|
208 |
+
|
209 |
+
|
210 |
+
def filter_empty_lines(text):
|
211 |
+
# Teile den Text in Zeilen auf
|
212 |
+
lines = text.split("\n")
|
213 |
+
|
214 |
+
# Filtere leere Zeilen heraus
|
215 |
+
non_empty_lines = filter(lambda line: line.strip() != "", lines)
|
216 |
+
|
217 |
+
# Verbinde die nicht leeren Zeilen zu einem String
|
218 |
+
filtered_text = "\n".join(non_empty_lines)
|
219 |
+
|
220 |
+
return filtered_text
|
221 |
+
|
222 |
+
|
223 |
+
def extract_article_links(**kwargs):
|
224 |
+
# print(len(kwargs["links"]))
|
225 |
+
with st.spinner("Extrahiere..."):
|
226 |
+
results = []
|
227 |
+
for link in kwargs["links"]:
|
228 |
+
results.append(extract_article(link))
|
229 |
+
st.session_state["extracted_articles"] = results
|
230 |
+
if st.session_state["process_step"] < 1:
|
231 |
+
st.session_state["process_step"] += 1
|
232 |
+
st.session_state["selected_page"] = 1
|
233 |
+
|
234 |
+
|
235 |
+
def extract_article_links_for_heading(**kwargs):
|
236 |
+
article = extract_article(kwargs["link"])
|
237 |
+
|
238 |
+
|
239 |
+
def finalize_articles():
|
240 |
+
final_articles = []
|
241 |
+
for i in range(len(st.session_state["extracted_articles"])):
|
242 |
+
final_articles.append(st.session_state["final_article_" + str(i + 1)])
|
243 |
+
st.session_state["final_articles"] = final_articles
|
244 |
+
if st.session_state["process_step"] < 2:
|
245 |
+
st.session_state["process_step"] += 1
|
246 |
+
st.session_state["selected_page"] += 1
|
247 |
+
|
248 |
+
|
249 |
+
def increase_page():
|
250 |
+
if st.session_state["selected_page"] <= st.session_state["process_step"]:
|
251 |
+
st.session_state["selected_page"] += 1
|
252 |
+
|
253 |
+
|
254 |
+
def decrease_page():
|
255 |
+
if st.session_state["selected_page"] > 0:
|
256 |
+
st.session_state["selected_page"] -= 1
|
257 |
+
|
258 |
+
|
259 |
+
def on_click_handler_generate_article(**kwargs):
|
260 |
+
with st.spinner("Generiere Artikel..."):
|
261 |
+
created_article = create_article(
|
262 |
+
kwargs["length_option"],
|
263 |
+
kwargs["final_articles"],
|
264 |
+
kwargs["add_info"],
|
265 |
+
kwargs["webpage_option"],
|
266 |
+
)
|
267 |
+
headline = create_headline(created_article, kwargs["webpage_option"])
|
268 |
+
st.session_state["generated_article"] = created_article
|
269 |
+
st.session_state["generated_headline"] = headline
|
270 |
+
if st.session_state["process_step"] < 3:
|
271 |
+
st.session_state["process_step"] += 1
|
272 |
+
st.session_state["selected_page"] += 1
|
273 |
+
|
274 |
+
|
275 |
+
def on_click_handler_generate_generate_article_keywords(**kwargs):
|
276 |
+
with st.spinner("Generiere Artikel..."):
|
277 |
+
created_article = create_article(
|
278 |
+
kwargs["length_option"],
|
279 |
+
"",
|
280 |
+
kwargs["artikel_input"],
|
281 |
+
kwargs["webpage_option"],
|
282 |
+
)
|
283 |
+
headline = create_headline(created_article, kwargs["webpage_option"])
|
284 |
+
st.session_state["generated_article"] = created_article
|
285 |
+
st.session_state["generated_headline"] = headline
|
286 |
+
|
287 |
+
|
288 |
+
def reset_session_state():
|
289 |
+
st.session_state["extracted_articles"] = []
|
290 |
+
st.session_state["article_links"] = []
|
291 |
+
st.session_state["final_articles"] = []
|
292 |
+
st.session_state["process_step"] = 0
|
293 |
+
st.session_state["selected_page"] = 0
|
294 |
+
st.session_state["generated_article"] = ""
|
295 |
+
st.session_state["studie_links"] = []
|
296 |
+
|
297 |
+
|
298 |
+
if "extracted_articles" not in st.session_state:
|
299 |
+
st.session_state["extracted_articles"] = []
|
300 |
+
if "article_links" not in st.session_state:
|
301 |
+
st.session_state["article_links"] = []
|
302 |
+
if "final_articles" not in st.session_state:
|
303 |
+
st.session_state["final_articles"] = []
|
304 |
+
if "process_step" not in st.session_state:
|
305 |
+
st.session_state["process_step"] = 0
|
306 |
+
if "selected_page" not in st.session_state:
|
307 |
+
st.session_state["selected_page"] = 0
|
308 |
+
if "generated_article" not in st.session_state:
|
309 |
+
st.session_state["generated_article"] = ""
|
310 |
+
if "function_state" not in st.session_state:
|
311 |
+
st.session_state["function_state"] = True
|
312 |
+
if "generated_headline" not in st.session_state:
|
313 |
+
st.session_state["generated_headline"] = ""
|
314 |
+
if "webpage_option" not in st.session_state:
|
315 |
+
st.session_state["webpage_option"] = "Boulevard"
|
316 |
+
if "studie_links" not in st.session_state:
|
317 |
+
st.session_state["studie_links"] = []
|
318 |
+
|
319 |
+
PROCESS_STEPS = [
|
320 |
+
"Artikel Extraktion",
|
321 |
+
"Artikel Finalisierung",
|
322 |
+
"Artikel Generierung",
|
323 |
+
"Artikel Ausgabe",
|
324 |
+
]
|
325 |
+
|
326 |
+
|
327 |
+
# def check_password():
|
328 |
+
# """Returns `True` if the user had the correct password."""
|
329 |
+
|
330 |
+
# def password_entered():
|
331 |
+
# """Checks whether a password entered by the user is correct."""
|
332 |
+
# if hmac.compare_digest(
|
333 |
+
# st.session_state["password"], os.environ.get("PASSWORD")
|
334 |
+
# ):
|
335 |
+
# st.session_state["password_correct"] = True
|
336 |
+
# del st.session_state["password"] # Don't store the password.
|
337 |
+
# else:
|
338 |
+
# st.session_state["password_correct"] = False
|
339 |
+
|
340 |
+
# # Return True if the password is validated.
|
341 |
+
# if st.session_state.get("password_correct", False):
|
342 |
+
# return True
|
343 |
+
|
344 |
+
# # Show input for password.
|
345 |
+
# st.text_input(
|
346 |
+
# "Password", type="password", on_change=password_entered, key="password"
|
347 |
+
# )
|
348 |
+
# if "password_correct" in st.session_state:
|
349 |
+
# st.error("😕 Password incorrect")
|
350 |
+
# return False
|
351 |
+
|
352 |
+
|
353 |
+
# if not check_password():
|
354 |
+
# st.stop() # Do not continue if check_password is not True.
|
355 |
+
|
356 |
+
col1, col2 = st.columns([2, 1])
|
357 |
+
|
358 |
+
col1.title("TINA")
|
359 |
+
col2.image("tensora_logo.png")
|
360 |
+
|
361 |
+
st.radio(
|
362 |
+
"Wähle den Schreibstil für Artikel aus",
|
363 |
+
["Boulevard", "Health Blog"],
|
364 |
+
key="webpage_option",
|
365 |
+
)
|
366 |
+
|
367 |
+
with st.sidebar:
|
368 |
+
st.title("Funktions Auswahl")
|
369 |
+
st.write("Hier kannst Du zwischen der Art der Artikelgenerierung wählen.")
|
370 |
+
st.button(
|
371 |
+
"Artikel Generierung mit Links",
|
372 |
+
key="article_gen_btn",
|
373 |
+
use_container_width=True,
|
374 |
+
on_click=lambda: st.session_state.update({"function_state": True}),
|
375 |
+
)
|
376 |
+
st.button(
|
377 |
+
"Artikel Generierung mit Stichpunkten",
|
378 |
+
key="headline_gen_btn",
|
379 |
+
use_container_width=True,
|
380 |
+
on_click=lambda: st.session_state.update({"function_state": False}),
|
381 |
+
)
|
382 |
+
|
383 |
+
if st.session_state["function_state"]:
|
384 |
+
tab_col1, tab_col2, tab_col3, tab_col4 = st.columns([1, 1, 1, 1])
|
385 |
+
|
386 |
+
tab_col1.button(
|
387 |
+
"Artikel Extraktion",
|
388 |
+
key="tab1",
|
389 |
+
use_container_width=True,
|
390 |
+
on_click=lambda: st.session_state.update({"selected_page": 0}),
|
391 |
+
disabled=st.session_state["selected_page"] == 0,
|
392 |
+
)
|
393 |
+
tab_col2.button(
|
394 |
+
"Artikel Finalisierung",
|
395 |
+
key="tab2",
|
396 |
+
use_container_width=True,
|
397 |
+
on_click=lambda: st.session_state.update({"selected_page": 1}),
|
398 |
+
disabled=st.session_state["process_step"] < 1
|
399 |
+
or st.session_state["selected_page"] == 1,
|
400 |
+
)
|
401 |
+
tab_col3.button(
|
402 |
+
"Artikel Generierung",
|
403 |
+
key="tab3",
|
404 |
+
use_container_width=True,
|
405 |
+
on_click=lambda: st.session_state.update({"selected_page": 2}),
|
406 |
+
disabled=st.session_state["process_step"] < 2
|
407 |
+
or st.session_state["selected_page"] == 2,
|
408 |
+
)
|
409 |
+
tab_col4.button(
|
410 |
+
"Artikel Ausgabe",
|
411 |
+
key="tab4",
|
412 |
+
use_container_width=True,
|
413 |
+
on_click=lambda: st.session_state.update({"selected_page": 3}),
|
414 |
+
disabled=st.session_state["process_step"] < 3
|
415 |
+
or st.session_state["selected_page"] == 3,
|
416 |
+
)
|
417 |
+
|
418 |
+
nav_col1, nav_col2, nav_col3 = st.columns([1, 4, 1])
|
419 |
+
|
420 |
+
nav_col1.button(
|
421 |
+
"◀️",
|
422 |
+
key="nav1",
|
423 |
+
use_container_width=True,
|
424 |
+
on_click=decrease_page,
|
425 |
+
disabled=st.session_state["selected_page"] == 0,
|
426 |
+
)
|
427 |
+
nav_col2.markdown(
|
428 |
+
f"<div style='text-align: center;'>{PROCESS_STEPS[st.session_state['selected_page']]}</div>",
|
429 |
+
unsafe_allow_html=True,
|
430 |
+
)
|
431 |
+
nav_col3.button(
|
432 |
+
"▶️",
|
433 |
+
key="nav2",
|
434 |
+
use_container_width=True,
|
435 |
+
on_click=increase_page,
|
436 |
+
disabled=st.session_state["selected_page"] == st.session_state["process_step"],
|
437 |
+
)
|
438 |
+
|
439 |
+
if st.session_state["selected_page"] == 0:
|
440 |
+
st.write(
|
441 |
+
"Bitte gebe die Links der Artikel ein, welche Du extrahiert haben möchtest."
|
442 |
+
)
|
443 |
+
st.text_input(
|
444 |
+
"Gebe den "
|
445 |
+
+ str(len(st.session_state["article_links"]) + 1)
|
446 |
+
+ ". Link ein:",
|
447 |
+
key="link_input_" + str(len(st.session_state["article_links"]) + 1),
|
448 |
+
)
|
449 |
+
if st.session_state[
|
450 |
+
"link_input_" + str(len(st.session_state["article_links"]) + 1)
|
451 |
+
]:
|
452 |
+
st.session_state["article_links"].append(
|
453 |
+
st.session_state[
|
454 |
+
"link_input_" + str(len(st.session_state["article_links"]) + 1)
|
455 |
+
]
|
456 |
+
)
|
457 |
+
st.rerun()
|
458 |
+
for i in range(len(st.session_state["article_links"])):
|
459 |
+
st.write(f"Link nr. {i+1}:\n\n{st.session_state['article_links'][i]}")
|
460 |
+
|
461 |
+
if len(st.session_state["article_links"]) > 0:
|
462 |
+
try:
|
463 |
+
st.button(
|
464 |
+
"Extrahiere Artikel",
|
465 |
+
on_click=extract_article_links,
|
466 |
+
kwargs={"links": st.session_state["article_links"]},
|
467 |
+
)
|
468 |
+
except Exception as e:
|
469 |
+
print(f"Fehler beim extrahieren der artikel: {str(e)}")
|
470 |
+
st.error(
|
471 |
+
f"Du hast einen oder mehrere Links nicht in dem korrekten Format angegeben. Bitte Lade die Seite neu und benutze korrekte Links: {str(e)}",
|
472 |
+
icon="🚨",
|
473 |
+
)
|
474 |
+
elif st.session_state["selected_page"] == 1:
|
475 |
+
st.write(
|
476 |
+
"Hier kannst Du die extrahierten Artikel ansehen und bei Bedarf anpassen."
|
477 |
+
)
|
478 |
+
for i, article in enumerate(st.session_state["extracted_articles"]):
|
479 |
+
with st.expander(f"Artikel {i+1}"):
|
480 |
+
if article:
|
481 |
+
st.text_area(
|
482 |
+
"Editiere die Artikel, falls nötig:",
|
483 |
+
value=article,
|
484 |
+
key="final_article_" + str(i + 1),
|
485 |
+
height=500,
|
486 |
+
)
|
487 |
+
else:
|
488 |
+
st.info(
|
489 |
+
"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.",
|
490 |
+
icon="ℹ️",
|
491 |
+
)
|
492 |
+
st.text_area(
|
493 |
+
"Füge den Artikel ein, falls nötig:",
|
494 |
+
value=article,
|
495 |
+
key="final_article_" + str(i + 1),
|
496 |
+
height=500,
|
497 |
+
)
|
498 |
+
st.button("Artikel finalisieren", on_click=finalize_articles)
|
499 |
+
elif st.session_state["selected_page"] == 2:
|
500 |
+
for i in range(len(st.session_state["final_articles"])):
|
501 |
+
if st.session_state["final_articles"][i]:
|
502 |
+
with st.expander("Artikel " + str(i + 1)):
|
503 |
+
st.write(st.session_state["final_articles"][i])
|
504 |
+
|
505 |
+
if len(st.session_state["final_articles"]) > 0:
|
506 |
+
st.write("Benutzte Artikel:")
|
507 |
+
for i, link in enumerate(st.session_state["article_links"]):
|
508 |
+
st.write(f"Link {i+1}: {link}")
|
509 |
+
st.text_area(
|
510 |
+
"Füge weitere Informationen für den Prompt hinzu, falls nötig:",
|
511 |
+
key="add_info",
|
512 |
+
)
|
513 |
+
st.write("Artikellänge")
|
514 |
+
st.radio("Optionen", ["Kurz", "Mittel", "Lang"], key="length_option")
|
515 |
+
st.button(
|
516 |
+
"Artikel generieren",
|
517 |
+
key="article_btn",
|
518 |
+
on_click=on_click_handler_generate_article,
|
519 |
+
kwargs={
|
520 |
+
"length_option": st.session_state["length_option"],
|
521 |
+
"final_articles": st.session_state["final_articles"],
|
522 |
+
"add_info": st.session_state["add_info"],
|
523 |
+
"webpage_option": st.session_state["webpage_option"],
|
524 |
+
},
|
525 |
+
)
|
526 |
+
elif st.session_state["selected_page"] == 3:
|
527 |
+
st.write(f"**{st.session_state['generated_headline']}**")
|
528 |
+
st.write(st.session_state["generated_article"])
|
529 |
+
st.write("Kopieren Sie den Artikel: ")
|
530 |
+
st_copy_to_clipboard(
|
531 |
+
st.session_state["generated_headline"]
|
532 |
+
+ "\n"
|
533 |
+
+ st.session_state["generated_article"]
|
534 |
+
)
|
535 |
+
if st.session_state["studie_links"]:
|
536 |
+
st.write("Hier sind einige Studien, die relevant sein könnten:")
|
537 |
+
for result in st.session_state["studie_links"]:
|
538 |
+
st.write(f"- [{result['title']}]({result['link']})")
|
539 |
+
else:
|
540 |
+
st.write("Keine relevanten Studien gefunden.")
|
541 |
+
st.button(
|
542 |
+
"Relevante Studien finden",
|
543 |
+
on_click=get_related_studies,
|
544 |
+
args=(st.session_state["generated_article"],),
|
545 |
+
)
|
546 |
+
st.button(
|
547 |
+
"Neuen Artikel generieren", key="reset_btn", on_click=reset_session_state
|
548 |
+
)
|
549 |
+
else:
|
550 |
+
st.write(
|
551 |
+
"Bitte trage die Stichpunkte ein, die Du in den Artikel einbauen möchtest. Der Textinput ist essenziell für die Generierung des Artikels."
|
552 |
+
)
|
553 |
+
st.text_area(label="Artikel input:", key="keyword_article_input")
|
554 |
+
st.write("Artikellänge")
|
555 |
+
st.radio("Optionen", ["Kurz", "Mittel", "Lang"], key="length_option")
|
556 |
+
st.button(
|
557 |
+
"Artikel generieren",
|
558 |
+
key="article_btn",
|
559 |
+
on_click=on_click_handler_generate_generate_article_keywords,
|
560 |
+
kwargs={
|
561 |
+
"length_option": st.session_state["length_option"],
|
562 |
+
"artikel_input": st.session_state["keyword_article_input"],
|
563 |
+
"webpage_option": st.session_state["webpage_option"],
|
564 |
+
},
|
565 |
+
)
|
566 |
+
if st.session_state["generated_article"] and st.session_state["generated_headline"]:
|
567 |
+
st.write(f"**{st.session_state['generated_headline']}**")
|
568 |
+
st.write(st.session_state["generated_article"])
|
569 |
+
st.write("Kopieren Sie den Artikel: ")
|
570 |
+
st_copy_to_clipboard(
|
571 |
+
st.session_state["generated_headline"]
|
572 |
+
+ "\n"
|
573 |
+
+ st.session_state["generated_article"]
|
574 |
+
)
|
575 |
+
if st.session_state["studie_links"]:
|
576 |
+
st.write("Hier sind einige Studien, die relevant sein könnten:")
|
577 |
+
for result in st.session_state["studie_links"]:
|
578 |
+
st.write(f"- [{result['title']}]({result['link']})")
|
579 |
+
# else:
|
580 |
+
# st.write("Keine relevanten Studien gefunden.")
|
581 |
+
st.button(
|
582 |
+
"Relevante Studien finden",
|
583 |
+
on_click=get_related_studies,
|
584 |
+
args=(st.session_state["generated_article"],),
|
585 |
+
)
|
586 |
+
st.button(
|
587 |
+
"Neuen Artikel generieren", key="reset_btn", on_click=reset_session_state
|
588 |
+
)
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
streamlit
|
2 |
openai==0.28.1
|
3 |
python-dotenv
|
4 |
requests
|
|
|
1 |
+
streamlit<=1.32.0
|
2 |
openai==0.28.1
|
3 |
python-dotenv
|
4 |
requests
|
tensora_logo.png
ADDED