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 gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import base64
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Set up Google GenAI API Key (Replace with your actual API key)
|
| 8 |
+
genai.configure(api_key="AIzaSyBZ_ehc2Q00pGiexfVYJmrN6-hxkEUHEMs")
|
| 9 |
+
|
| 10 |
+
def image_to_tamil_poem(image):
|
| 11 |
+
"""Generates a Tamil poem about an upload image using Gemini 1.5 Pro"""
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
# Convert image to bytes
|
| 15 |
+
buffered = BytesIO()
|
| 16 |
+
image.save(buffered, format="PNG")
|
| 17 |
+
img_bytes = buffered.getvalue()
|
| 18 |
+
|
| 19 |
+
# Convert image to Base64 for Gemini API
|
| 20 |
+
image_b64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 21 |
+
prompt = "Describe this image in one sentence."
|
| 22 |
+
|
| 23 |
+
# Use Gemini 1.5 Pro for image analysis
|
| 24 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 25 |
+
response = model.generate_content([{"mime_type": "image/png", "data": image_b64}, prompt])
|
| 26 |
+
description = response.text if response else "No description available."
|
| 27 |
+
|
| 28 |
+
# Generate Tamil poem based on the description
|
| 29 |
+
response_poem = model.generate_content(f"Based on this image description: {description}, write a short poem in Tamil.")
|
| 30 |
+
tamil_poem = response_poem.text if response_poem else "கவிதை உருவாக்கப்படவில்லை."
|
| 31 |
+
|
| 32 |
+
return tamil_poem
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"⚠️ Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Define Gradio Interface
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=image_to_tamil_poem,
|
| 40 |
+
inputs=gr.Image(type="pil"),
|
| 41 |
+
outputs=gr.Textbox(label="Generated Tamil Poem"),
|
| 42 |
+
title="AI-Powered Tamil Poem Generator (Using Gemini AI)",
|
| 43 |
+
description="Upload an image, and AI will generate a short Tamil poem based on it."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
google-generativeai
|
| 3 |
+
pillow
|