import gradio as gr import requests import os import json import google.generativeai as genai from bs4 import BeautifulSoup from groq import Groq # Load environment variables genai.configure(api_key=os.environ["geminiapikey"]) read_key = os.environ.get('HF_TOKEN', None) cx="77f1602c0ff764edb" custom_css = """ #md { height: 400px; font-size: 30px; background: #202020; padding: 20px; color: white; border: 1 px solid white; } """ api_key = os.getenv('groq') google_api_key = os.getenv('google_search') if api_key is None: raise ValueError("groq_whisper environment variable is not set") # Initialize the Groq client client = Groq(api_key=api_key) def perform_search(prompt): if prompt.strip() == '': return '' # Return empty string for empty search # URL der Google Custom Search API url = f"https://www.googleapis.com/customsearch/v1?key={google_api_key}&cx={cx}&q={prompt}" try: # HTTP GET-Anfrage an die Google Custom Search API response = requests.get(url) response.raise_for_status() # Wirft eine Exception, wenn die Anfrage fehlschlägt # JSON-Antwort parsen data = response.json() # Extrahiere die Suchergebnisse items = data.get('items', []) results = [item['snippet'] for item in items] #return results[0] # Kombiniere die Ergebnisse zu einem String result_text = '\n'.join(results) return result_text # Formuliere die Antwort search_query = f"{prompt} antworte kurz und knapp. antworte auf deutsch. du findest die antwort hier: {result_text}" result = predict(search_query) return result return search_query except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") return '' 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=[] ) response = chat_session.send_message(f"{prompt}\n antworte immer auf deutsch") response_value = response.candidates[0].content.parts[0].text return response_value #very simple (and extremly fast) websearch def websearch(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" } url = f"https://www.google.com/search?q={search_term}" response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') response_text = soup.find('body') prompt = f"{search_term}\n use this result from a google search to answer the question \n {response_text.text}" result = predict(prompt) return result def process_audio(file_path): try: # Open the audio file with open(file_path, "rb") as file: # Create a translation of the audio file translation = client.audio.transcriptions.create( file=(os.path.basename(file_path), file.read()), # Correct passing of filename model="whisper-large-v3-turbo", # Required model to use for translation prompt="transcribe", # Optional language="de", # Optional response_format="json", # Optional temperature=0.0 # Optional ) # Return the translation text suche = websearch(translation.text) result = predict(suche) return result return translation.text except Exception as e: return f"An error occurred: {str(e)}" # 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...") #audio_input=gr.Microphone(type="filepath") with gr.Row(): button = gr.Button("Senden") # Connect the button to the function button.click(fn=perform_search, inputs=ort_input, outputs=details_output) # Launch the Gradio application demo.launch() """ with gr.Blocks() as speech: with gr.Row(): sr_outputs = gr.Textbox(label="Antwort") with gr.Row(): sr_inputs = gr.Microphone(type="filepath") sr_inputs.change(process_audio, inputs=sr_inputs, outputs=sr_outputs) speech.launch() """