File size: 1,340 Bytes
ec31001
e26d3af
ec31001
 
ba0742d
ec31001
 
 
ba0742d
ec31001
ba0742d
ec31001
 
ba0742d
e26d3af
 
ba0742d
e26d3af
1318943
e26d3af
 
ec31001
 
ba0742d
ec31001
ba0742d
ec31001
 
ba0742d
 
 
 
ec31001
 
ba0742d
ec31001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()