Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +47 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("GEMINI_API_KEY")) # 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"],
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
google-generativeai
|
2 |
+
gradio
|
3 |
+
pillow
|