import gradio as gr from google import genai import os # 1. Fetch the API Key from the environment (configured in Settings -> Secrets) GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") def generate_response(prompt): # Check if the API Key is provided if not GOOGLE_API_KEY: return "⚠️ Error: API Key not found! Please configure GOOGLE_API_KEY in the Settings -> Variables and secrets page of your Space." try: # 2. Initialise the Client using the new SDK client = genai.Client(api_key=GOOGLE_API_KEY) # 3. Send the prompt to Google AI Studio for processing response = client.models.generate_content( model='gemma-4-31b-it', contents=prompt ) return response.text except Exception as e: return f"❌ An error occurred: {str(e)}" # 4. Build the Gradio UI (Error fixed by removing allow_flagging) demo = gr.Interface( fn=generate_response, inputs=gr.Textbox(lines=4, placeholder="Type your prompt here...", label="Prompt"), outputs=gr.Textbox(label="Response from Model"), title="⚡ My Gemini/Gemma App (Hosted on HF)", description="This application is hosted for free on Hugging Face Spaces, powered by the Google API using the gemma-4-31b-it model." ) # Launch the web application demo.launch()