import gradio as gr import os import google.generativeai as genai import logging import time import backoff # Configure Logging logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s') # Load environment variables try: genai.configure(api_key=os.environ["geminiapikey"]) except KeyError: logging.error("Error: 'geminiapikey' environment variable not found.") exit(1) read_key = os.environ.get('HF_TOKEN', None) custom_css = """ #md { height: 400px; font-size: 30px; background: #202020; padding: 20px; color: white; border: 1px solid white; } """ @backoff.on_exception(backoff.expo, (genai.APIError), max_tries=3) # retry up to 3 times def predict(prompt): # Create the model generation_config = { "temperature": 0.7, "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", generation_config=generation_config, ) try: contents_to_send = [genai.Content(parts=[prompt])] response = model.generate_content(contents=contents_to_send, tools='google_search_retrieval') if response and response.text: return response.text else: logging.error(f"Unexpected response: {response}") return "Error: Could not extract text from the response." except genai.APIError as e: logging.error(f"API error occurred: {e}") raise except Exception as e: logging.error(f"An error occurred: {e}") return f"An error occurred: {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") 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=predict, inputs=ort_input, outputs=details_output) # Launch the Gradio application demo.launch()