| import gradio as gr |
| from google import genai |
| import os |
|
|
| |
| GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") |
|
|
| def generate_response(prompt): |
| |
| 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: |
| |
| client = genai.Client(api_key=GOOGLE_API_KEY) |
| |
| |
| 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)}" |
|
|
| |
| 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." |
| ) |
|
|
| |
| demo.launch() |