PerplexicaApi / app.py
mgokg's picture
Update app.py
a07a31e verified
raw
history blame
2.37 kB
import gradio as gr
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
def parse_links(ort):
try:
# Konstruiere die vollständige URL
initial_url = "http://specialist-it.de:3000?q=hallo"
import requests
url = "http://specialist-it.de:3001/api/search"
payload = {
"chatModel": {
"provider": "groq",
"model": "LLaMA3-70b"
},
"embeddingModel": {
"provider": "Local",
"model": "BGE small"
},
"optimizationMode": "balanced",
"focusMode": "webSearch",
"query": "What is Perplexica",
"history": [
["human", "Hi, how are you?"],
["assistant", "I am doing well, how can I help you today?"]
]
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
return response.json()
# Senden der Anfrage an die initiale URL
response = requests.get(initial_url)
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Den body-Tag finden
body_tag = soup.find('body')
# Den Text des body-Tags zurückgeben
body_text = body_tag.get_text()
# Den Text ausgeben
print(body_text)
return body_text
# Rückgabe als Dictionary, um es mit gr.JSON kompatibel zu machen
#return {"body_text": body_text}
except requests.RequestException as e:
return {"error": str(e)}
except Exception as e:
return {"error": str(e)}
# Erstelle die Gradio-Schnittstelle
with gr.Blocks() as demo:
gr.Markdown("# Vereine in Bayern")
ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
links_output = gr.JSON(label="Gefundene Vereine")
# Button zum Starten der Parsung
button = gr.Button("Parse und Scrape")
# Verbinde den Button mit der Funktion
button.click(fn=parse_links, inputs=ort_input, outputs=links_output)
demo.launch()