Spaces:
Running
Running
import gradio as gr | |
import requests | |
import os | |
import json | |
import google.generativeai as genai | |
from bs4 import BeautifulSoup | |
# Load environment variables | |
genai.configure(api_key=os.environ["geminiapikey"]) | |
read_key = os.environ.get('HF_TOKEN', None) | |
custom_css = """ | |
#md { | |
height: 400px; | |
font-size: 30px; | |
background: #202020; | |
padding: 20px; | |
color: white; | |
border: 1 px solid white; | |
} | |
""" | |
def predict(prompt): | |
generation_config = { | |
"temperature": 0.4, | |
"top_p": 0.95, | |
"top_k": 40, | |
"max_output_tokens": 8192, | |
"response_mime_type": "text/plain", | |
} | |
model = genai.GenerativeModel( | |
model_name="gemini-2.0-flash-exp", | |
generation_config=generation_config, | |
) | |
chat_session = model.start_chat( | |
history=[ | |
{ | |
"role": "user", | |
"parts": [ | |
"return a json object with the contact details. leave blank if information is not available. here is the json schema:\n\n{\n \"organization\": \"\",\n \"address\": \"\",\n \"phone\": \"\",\n \"email\": \"\",\n \"website\": \"\"\n}\n\nyou can find the contact details here: \nImpressum – Aero-Club Bamberg e.V.\nAero-Club Bamberg\nhttps://aeroclub-bamberg.de › impressum\nAero-Club Bamberg e.V.. Zeppelinstraße 18. D-96052 Bamberg. Tel.: 0951 / 45 1 45. Fax: 0951 / 13 22 20. E-Mail: [email protected].\n", | |
], | |
}, | |
{ | |
"role": "model", | |
"parts": [ | |
"```json\n{\n \"organization\": \"Aero-Club Bamberg e.V.\",\n \"address\": \"Zeppelinstraße 18, D-96052 Bamberg\",\n \"phone\": \"0951 / 45 1 45\",\n \"email\": \"[email protected]\",\n \"website\": \"https://aeroclub-bamberg.de\"\n}\n```\n", | |
], | |
}, | |
] | |
) | |
response = chat_session.send_message("return a json object with the contact details. leave blank if information is not available. here is the json schema:\n\n{\n \"organization\": \"\",\n \"address\": \"\",\n \"phone\": \"\",\n \"email\": \"\",\n \"website\": \"\"\n}\n\nyou can find the contact details here: \n" + prompt) | |
return response | |
def get_impressum_text(search_term): | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
} | |
vereine = [] | |
#search_results = google_search(search_term) | |
url = f"https://www.google.com/search?q=mpressum {search_term}" | |
response = requests.get(url, headers=headers) | |
soup = BeautifulSoup(response.content, 'html.parser') | |
impressum_div = soup.find('body') | |
json_data = predict(impressum_div.text) | |
vereine.append(json_data) | |
return vereine | |
def websearch(prompt): | |
url = f"https://www.google.com/search?q={prompt}" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() # Wirft eine Exception für Fehlercodes | |
except requests.exceptions.RequestException as e: | |
print(f"Fehler beim Abrufen der Google-Seite: {e}") | |
return None | |
soup = BeautifulSoup(response.content, 'html.parser') | |
first_div = soup.find('div', class_='MjjYud') | |
if first_div: | |
return first_div.text.strip() | |
else: | |
print("Kein div mit der Klasse 'MjjYud' gefunden.") | |
return None | |
# Create the Gradio interface | |
with gr.Blocks(css=custom_css) as demo: | |
with gr.Row(): | |
#details_output = gr.Markdown(label="answer", elem_id="md") | |
details_output = gr.Textbox(label="Ausgabe", value = f"\n\n\n\n") | |
with gr.Row(): | |
ort_input = gr.Textbox(label="prompt", placeholder="ask anything...") | |
with gr.Row(): | |
button = gr.Button("Senden") | |
# Connect the button to the function | |
button.click(fn=get_impressum_text, inputs=ort_input, outputs=details_output) | |
# Launch the Gradio application | |
demo.launch() |