Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import openai
|
| 5 |
+
import csv
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def add_to_csv(var1, var2, var3, var4, var5, var6, var7, filename):
|
| 10 |
+
# Öffnen der CSV-Datei im Anhängemodus
|
| 11 |
+
with open(filename, 'a', newline='', encoding = "utf-8") as file:
|
| 12 |
+
writer = csv.writer(file)
|
| 13 |
+
|
| 14 |
+
# Hinzufügen der Variablen als neue Zeile in die CSV-Datei
|
| 15 |
+
writer.writerow([var1, var2, var3, var4, var5, var6, var7])
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Definiert die Funktion zum Scrapen der Webseite
|
| 19 |
+
def scrape_website(url):
|
| 20 |
+
try:
|
| 21 |
+
response = requests.get(url)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 24 |
+
content = {
|
| 25 |
+
'scraped_html': ' '.join(str(p) for p in soup.find_all('p', class_="sc-beqWaB jOAegM")),
|
| 26 |
+
'heute_überschrift': ' '.join(str(p) for p in soup.find_all('h1', class_="sc-beqWaB iTcspr")),
|
| 27 |
+
'heute_zsm': ' '.join(str(p) for p in soup.find_all('p', class_="sc-beqWaB iOdRIJ"))
|
| 28 |
+
}
|
| 29 |
+
return content
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return str(e)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def send_to_chatgpt(api_key, prompt_file, transcript):
|
| 35 |
+
try:
|
| 36 |
+
# Liest den Prompt aus der Datei
|
| 37 |
+
with open(prompt_file, 'r', encoding='utf-8') as file:
|
| 38 |
+
prompt = file.read().strip()
|
| 39 |
+
|
| 40 |
+
openai.api_key = api_key
|
| 41 |
+
response = openai.ChatCompletion.create(
|
| 42 |
+
model="gpt-4-1106-preview",
|
| 43 |
+
messages=[
|
| 44 |
+
{"role": "system", "content": prompt},
|
| 45 |
+
{"role": "system", "content": transcript}
|
| 46 |
+
],
|
| 47 |
+
)
|
| 48 |
+
return response.choices[0].message["content"]
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return str(e)
|
| 51 |
+
|
| 52 |
+
st.title("Webseiten-Scraper")
|
| 53 |
+
# Beispiel für die Verwendung der Funktion
|
| 54 |
+
|
| 55 |
+
api_key = os.getenv("api_key") # Setzen Sie hier Ihren OpenAI-API-Schlüssel ein
|
| 56 |
+
base_prompt = "txt.txt"
|
| 57 |
+
csv_name = "results.csv"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Eingabefelder für URL und p_class
|
| 61 |
+
url = st.text_input("URL eingeben", "https://www.beispielwebsite.com")
|
| 62 |
+
|
| 63 |
+
if st.button("Scrape"):
|
| 64 |
+
if url:
|
| 65 |
+
scraped_html = scrape_website(url)
|
| 66 |
+
response = send_to_chatgpt(api_key, base_prompt, scraped_html['scraped_html'])
|
| 67 |
+
st.code(response)
|
| 68 |
+
|
| 69 |
+
teile = response.split("Kernaussagen:")
|
| 70 |
+
ueberschriften_teil, kernaussagen_teil = teile[0], teile[1]
|
| 71 |
+
|
| 72 |
+
ueberschriften = ueberschriften_teil.split("\n")[1:] # Erste Zeile überspringen
|
| 73 |
+
ueberschrift_1 = ueberschriften[0].split(" ", 1)[1] # "1." entfernen
|
| 74 |
+
ueberschrift_2 = ueberschriften[1].split(" ", 1)[1] # "2." entfernen
|
| 75 |
+
|
| 76 |
+
kernaussagen = kernaussagen_teil.split("\n")[1:] # Erste Zeile überspringen
|
| 77 |
+
kernaussage_1 = kernaussagen[0].split(" ", 1)[1] # "1." entfernen
|
| 78 |
+
kernaussage_2 = kernaussagen[1].split(" ", 1)[1] # "2." entfernen
|
| 79 |
+
|
| 80 |
+
add_to_csv(
|
| 81 |
+
url, scraped_html['heute_überschrift'], ueberschrift_1, ueberschrift_2, scraped_html['heute_zsm'],
|
| 82 |
+
kernaussage_1, kernaussage_2, csv_name
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
st.error("Bitte geben Sie eine gültige URL ein.")
|