Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,20 @@ 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(
|
| 12 |
writer = csv.writer(file)
|
| 13 |
|
| 14 |
# Hinzufügen der Variablen als neue Zeile in die CSV-Datei
|
|
@@ -22,23 +31,29 @@ def scrape_website(url):
|
|
| 22 |
response.raise_for_status()
|
| 23 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 24 |
content = {
|
| 25 |
-
'scraped_html': ' '.join(
|
| 26 |
-
'heute_überschrift': ' '.join(
|
| 27 |
-
'heute_zsm': ' '.join(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
openai.api_key = api_key
|
| 38 |
response = openai.ChatCompletion.create(
|
| 39 |
model="gpt-4-1106-preview",
|
| 40 |
messages=[
|
| 41 |
-
{"role": "system", "content":
|
| 42 |
{"role": "system", "content": transcript}
|
| 43 |
],
|
| 44 |
)
|
|
@@ -47,8 +62,7 @@ def send_to_chatgpt(api_key, prompt_file, transcript):
|
|
| 47 |
return str(e)
|
| 48 |
|
| 49 |
st.title("Webseiten-Scraper")
|
| 50 |
-
# Beispiel für die Verwendung der
|
| 51 |
-
|
| 52 |
api_key = os.getenv("api_key") # Setzen Sie hier Ihren OpenAI-API-Schlüssel ein
|
| 53 |
base_prompt = os.getenv("prompt")
|
| 54 |
csv_name = "results.csv"
|
|
@@ -81,3 +95,35 @@ if st.button("Scrape"):
|
|
| 81 |
|
| 82 |
else:
|
| 83 |
st.error("Bitte geben Sie eine gültige URL ein.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import openai
|
| 5 |
import csv
|
| 6 |
import os
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import random
|
| 9 |
+
from huggingface_hub import Repository, HfApi, HfFolder
|
| 10 |
|
| 11 |
+
api = HfApi()
|
| 12 |
+
token = os.getenv("token")
|
| 13 |
+
tokenread = os.getenv("tokenread")
|
| 14 |
+
localdir = "HeadlinePrediction"
|
| 15 |
+
|
| 16 |
+
repo = Repository(local_dir=localdir, clone_from="https://huggingface.co/spaces/Add1E/HeadlinePrediction", token=token)
|
| 17 |
|
| 18 |
def add_to_csv(var1, var2, var3, var4, var5, var6, var7, filename):
|
| 19 |
# Öffnen der CSV-Datei im Anhängemodus
|
| 20 |
+
with open(os.path.abspath(f'{localdir}/results.csv'), 'a', newline='', encoding = "utf-8") as file:
|
| 21 |
writer = csv.writer(file)
|
| 22 |
|
| 23 |
# Hinzufügen der Variablen als neue Zeile in die CSV-Datei
|
|
|
|
| 31 |
response.raise_for_status()
|
| 32 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 33 |
content = {
|
| 34 |
+
'scraped_html': ' '.join(p.get_text() for p in soup.find_all('p', class_="sc-beqWaB jOAegM")),
|
| 35 |
+
'heute_überschrift': ' '.join(p.get_text() for p in soup.find_all('h1', class_="sc-beqWaB iTcspr")),
|
| 36 |
+
'heute_zsm': ' '.join(p.get_text() for p in soup.find_all('p', class_="sc-beqWaB iOdRIJ"))
|
| 37 |
}
|
| 38 |
return content
|
| 39 |
except Exception as e:
|
| 40 |
return str(e)
|
| 41 |
|
| 42 |
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
def send_to_chatgpt(api_key, prompt_file, transcript):
|
| 47 |
try:
|
| 48 |
+
# Liest den Prompt aus der Datei
|
| 49 |
+
with open(prompt_file, 'r', encoding='utf-8') as file:
|
| 50 |
+
prompt = file.read().strip()
|
| 51 |
+
|
| 52 |
openai.api_key = api_key
|
| 53 |
response = openai.ChatCompletion.create(
|
| 54 |
model="gpt-4-1106-preview",
|
| 55 |
messages=[
|
| 56 |
+
{"role": "system", "content": prompt},
|
| 57 |
{"role": "system", "content": transcript}
|
| 58 |
],
|
| 59 |
)
|
|
|
|
| 62 |
return str(e)
|
| 63 |
|
| 64 |
st.title("Webseiten-Scraper")
|
| 65 |
+
# Beispiel für die Verwendung der Funktiona
|
|
|
|
| 66 |
api_key = os.getenv("api_key") # Setzen Sie hier Ihren OpenAI-API-Schlüssel ein
|
| 67 |
base_prompt = os.getenv("prompt")
|
| 68 |
csv_name = "results.csv"
|
|
|
|
| 95 |
|
| 96 |
else:
|
| 97 |
st.error("Bitte geben Sie eine gültige URL ein.")
|
| 98 |
+
|
| 99 |
+
if st.sidebar.button("Upload Data"):
|
| 100 |
+
repo.git_add(os.path.abspath(f'{localdir}/results.csv'))
|
| 101 |
+
repo.git_commit("Add new headlines.csv")
|
| 102 |
+
repo.git_push()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
auswahl = st.sidebar.selectbox("Wählen Sie eine Prediction:", ["None", "Prediction 1", "Prediction 2", "Prediction 3", "Prediction 4", "Prediction 5"])
|
| 106 |
+
|
| 107 |
+
random_numbers = set()
|
| 108 |
+
while len(random_numbers) < 5:
|
| 109 |
+
random_numbers.add(random.randint(0, len(df)-1))
|
| 110 |
+
|
| 111 |
+
random_numbers = list(random_numbers)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
if auswahl == "Prediction 1":
|
| 115 |
+
st.dataframe(df.iloc[random_numbers[0]])
|
| 116 |
+
elif auswahl == "Prediction 2":
|
| 117 |
+
st.dataframe(df.iloc[random_numbers[1]])
|
| 118 |
+
elif auswahl == "Prediction 3":
|
| 119 |
+
st.dataframe(df.iloc[random_numbers[2]])
|
| 120 |
+
elif auswahl == "Prediction 4":
|
| 121 |
+
st.dataframe(df.iloc[random_numbers[3]])
|
| 122 |
+
elif auswahl == "Prediction 5":
|
| 123 |
+
st.dataframe(df.iloc[random_numbers[4]])
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if st.sidebar.button("Show Full Data"):
|
| 128 |
+
df = pd.read_csv(csv_name, encoding='utf-8')
|
| 129 |
+
st.dataframe(df)
|