File size: 1,770 Bytes
4d032f5
7720e6d
da2da1a
 
7fa0bc1
b2771e3
7fa0bc1
7720e6d
 
 
a9194fa
b2771e3
7720e6d
 
9c1a222
7720e6d
 
4d032f5
a9194fa
 
 
 
7fa0bc1
5d8fadb
 
 
a9194fa
 
5d8fadb
a9194fa
 
 
5d8fadb
a9194fa
 
5d8fadb
a9194fa
0287c01
 
5d8fadb
 
0287c01
 
fc61b18
5d8fadb
0287c01
da2da1a
4d032f5
0287c01
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()