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 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 | |
def predict(prompt): | |
# Create the model | |
generation_config = { | |
"temperature": 0.3, | |
"top_p": 0.95, | |
"top_k": 40, | |
"max_output_tokens": 2048, | |
"response_mime_type": "text/plain", | |
} | |
model = genai.GenerativeModel( | |
#model_name="gemini-1.5-pro", | |
model_name="gemini-2.0-flash-exp", | |
generation_config=generation_config, | |
) | |
chat_session = model.start_chat( | |
history=[ | |
] | |
) | |
response = chat_session.send_message(prompt) | |
#response = model.generate_content(contents=prompt, tools='google_search_retrieval') | |
return response.text | |
# 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=websearch, inputs=ort_input, outputs=details_output) | |
# Launch the Gradio application | |
demo.launch() |