Spaces:
Running
Running
import gradio as gr | |
from google import genai | |
import os | |
# Load API key from environment variable | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
# Initialize the Google GenAI client | |
client = genai.Client(api_key=GEMINI_API_KEY) | |
# Function to interact with the Gemini API | |
def generate_content(prompt): | |
# Generate content using the Gemini API | |
response = client.models.generate_content( | |
model="gemini-2.5-pro-exp-03-25", | |
contents=[prompt] | |
) | |
# Format the result for better readability | |
result_text = response.text.strip() # Remove extra spaces and line breaks | |
formatted_response = f"**AI Generated Content:**\n\n{result_text}" | |
return formatted_response | |
# Create Gradio interface using Blocks for better control | |
with gr.Blocks() as interface: | |
# Title and Description | |
gr.Markdown("# Gemini AI Content Generator") | |
gr.Markdown("Provide a prompt and get an explanation from Gemini AI.\n\nThe AI will return a well-formatted response.") | |
# Text input field and submit button | |
prompt_input = gr.Textbox(label="Enter your prompt:", placeholder="How does AI work?", lines=2) | |
submit_button = gr.Button("Generate Content") | |
# Output area for the result | |
output_area = gr.Markdown() | |
# Loading spinner (show loading during API call) | |
loading_spinner = gr.HTML("<p style='color: #4CAF50;'>Loading... Please wait...</p>") | |
# Define button click interaction | |
def on_submit(prompt): | |
# Show the spinner when the button is clicked | |
return gr.update(visible=True), generate_content(prompt) | |
# Link the button with the function | |
submit_button.click(on_submit, inputs=prompt_input, outputs=[loading_spinner, output_area]) | |
# Launch Gradio interface | |
interface.launch() | |