Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set up Gemini API key
|
| 7 |
+
genai.configure(api_key=os.getenv("AIzaSyAPuP4uYtWWJBzw4OsxUDltGBGN9SYXK7Y")) # Replace with your API key
|
| 8 |
+
|
| 9 |
+
def generate_poem(image, prompt, language):
|
| 10 |
+
"""Generate a poem based on the image and prompt, optimized for speed."""
|
| 11 |
+
if not language:
|
| 12 |
+
return "Please select a language."
|
| 13 |
+
|
| 14 |
+
model = genai.GenerativeModel("gemini-1.5-flash") # Use a faster model
|
| 15 |
+
|
| 16 |
+
# Convert and resize the image for faster processing
|
| 17 |
+
img = image.convert("RGB")
|
| 18 |
+
img = img.resize((256, 256))
|
| 19 |
+
|
| 20 |
+
# Optimized short prompt
|
| 21 |
+
full_prompt = f"Generate a short poem in {language} based on this image and theme: {prompt}."
|
| 22 |
+
|
| 23 |
+
# Generate poem without streaming
|
| 24 |
+
response = model.generate_content([img, full_prompt])
|
| 25 |
+
output_text = response.text
|
| 26 |
+
|
| 27 |
+
return output_text
|
| 28 |
+
|
| 29 |
+
# Gradio UI
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=generate_poem,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Image(type="pil"), # Directly loads as PIL object
|
| 34 |
+
gr.Textbox(label="Enter a theme for the poem (in English)"),
|
| 35 |
+
gr.Dropdown(
|
| 36 |
+
["Hindi", "Tamil", "Telugu", "Malayalam", "Kannada", "Marathi", "Bengali","Punjabi","Spanish","French","Japanese","German","Russian"],
|
| 37 |
+
label="Select Output Language"
|
| 38 |
+
)
|
| 39 |
+
],
|
| 40 |
+
outputs="text",
|
| 41 |
+
title="Multilingual Image Poetry Generator",
|
| 42 |
+
description="Upload an image, enter a theme in English, and get a poem in your chosen regional language."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Run the app
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
iface.launch()
|
| 48 |
+
|