from datetime import datetime import os import uuid import openai import requests import streamlit as st from azure.cosmos import ContainerProxy, CosmosClient from bs4 import BeautifulSoup, NavigableString from dotenv import load_dotenv from st_copy_to_clipboard import st_copy_to_clipboard from pytrends.request import TrendReq import pytz import xml.etree.ElementTree as ET import re load_dotenv() st.set_page_config(initial_sidebar_state="collapsed") def get_related_studies(article: str): with st.spinner("Extrahiere Studien..."): url = f'https://serpapi.com/search.json?engine=google_scholar&api_key={os.getenv("SERP_API_KEY")}&as_ylo=2018&q=' url += extract_scholar_query(article).replace('"', "") try: response = requests.get(url) if response.status_code == 200: data = response.json() if data.get("organic_results"): results = [] for result in data["organic_results"]: if not result.get("title"): continue if not result.get("link"): continue results.append( { "title": result["title"], "link": result["link"], } ) st.session_state["studie_links"] = results else: st.session_state["studie_links"] = [] else: st.session_state["studie_links"] = [] except Exception as e: print(f"Fehler beim extrahieren der Studien: {str(e)}") st.error(f"Something went wrong: {str(e)}", icon="🚨") def get_takeaways(article: str): 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") takeaway_query = os.environ.get("takeaway") with st.spinner("Creating Takeaways"): try: res = openai.ChatCompletion.create( engine="gpt-4-1106", temperature=0.2, messages=[ { "role": "system", "content": f" The article you have written is as follows: {article}.", }, { "role": "system", "content": f"Schreibe mir zu diesen Artikel Key Takeaways nach folgenden Regeln {takeaway_query}.", }, ], ) st.session_state["takeaways"] = res["choices"][0]["message"]["content"] except Exception as e: print(f"Fehler beim extrahieren der Query: {str(e)}") st.error(f"Something went wrong: {str(e)}", icon="🚨") def get_faq(article: str): 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") faq_query = os.environ.get("faq") with st.spinner("Creating FAQ"): try: res = openai.ChatCompletion.create( engine="gpt-4-1106", temperature=0.2, messages=[ { "role": "system", "content": f" The article you have written is as follows: {article}.", }, { "role": "system", "content": f"Schreibe mir zu diesen Artikel Frequently Asked Questions nach folgenden Regeln {faq_query}.", }, ], ) st.session_state["faq"] = res["choices"][0]["message"]["content"] except Exception as e: print(f"Fehler beim extrahieren der Query: {str(e)}") st.error(f"Something went wrong: {str(e)}", icon="🚨") def extract_scholar_query(article: str): 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") try: res = openai.ChatCompletion.create( engine="gpt-4-1106", temperature=0.2, messages=[ { "role": "system", "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}.", } ], ) return res["choices"][0]["message"]["content"] except Exception as e: print(f"Fehler beim extrahieren der Query: {str(e)}") st.error(f"Something went wrong: {str(e)}", icon="🚨") return "" def create_article(length_option, articles, params, web_page_option): if length_option == "Kurz": length = os.environ.get("SHORT_LENGTH") elif length_option == "Mittel": length = os.environ.get("MEDIUM_LENGTH") elif length_option == "Lang": length = os.environ.get("LONG_LENGTH") elif length_option == "SEO": length = os.environ.get("SEO_LENGTH") elif length_option == "SEO Plus": length = os.environ.get("SEO_PLUS_LENGTH") 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 == "Boulevard": writing_style = os.environ.get("WRITING_STYLE_HEUTE") elif web_page_option == "Health Blog": writing_style = os.environ.get("WRITING_STYLE_GESUND") elif web_page_option == "Newspaper": writing_style = os.environ.get("WRITING_STYLE_NEWSPAPER") elif web_page_option == "Tech/Lifestyle Blog": writing_style = os.environ.get("WRITING_STYLE_TECH_BLOG") elif web_page_option == "Public Relations": writing_style = os.environ.get("WRITING_STYLE_PR") elif web_page_option == "Sales": writing_style = os.environ.get("WRITING_STYLE_SALES") elif web_page_option == "Lifestyle Blog": writing_style = os.environ.get("WRITING_STYLE_LIFESTYLE") try: if len(articles) > 0: article_string = "; ".join( f"Artikel {index + 1}: {artikel}" for index, artikel in enumerate(articles) ) 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.", }, {"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": f" It is very important that the length of your article you generate should be {length} words long.", }, { "role": "system", "content": "Schreibe den Artikel immer in deutscher Sprache.", }, ] else: messages = [ { "role": "system", "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} .It is important that the length of your article should be {length} words long.", }, { "role": "system", "content": f"Please write the article based on the following user input: {params}", }, { "role": "system", "content": "Schreibe den Artikel immer in deutscher Sprache.", }, ] res = openai.ChatCompletion.create( engine="gpt-35-16k", temperature=0.4, max_tokens=8000, messages=messages, ) 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 == "Boulevard": 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

,