Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
from PIL import Image
|
| 4 |
-
import os
|
| 5 |
import io
|
| 6 |
|
| 7 |
# Initialize Gemini
|
| 8 |
-
GOOGLE_API_KEY = "AIzaSyBdz-qcLFRDsR-mm37AlRf2w6RZws2lDL0" #
|
| 9 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 10 |
|
|
|
|
| 11 |
def process_image(image):
|
| 12 |
try:
|
| 13 |
prompt = """
|
|
@@ -30,23 +30,16 @@ def process_image(image):
|
|
| 30 |
"max_output_tokens": 1024,
|
| 31 |
}
|
| 32 |
|
| 33 |
-
# Convert
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
else:
|
| 38 |
-
# Convert PIL Image to bytes
|
| 39 |
-
image_bytes = io.BytesIO()
|
| 40 |
-
image.save(image_bytes, format='PNG')
|
| 41 |
-
image_data = image_bytes.getvalue()
|
| 42 |
|
| 43 |
-
# Create content parts
|
| 44 |
content_parts = [
|
| 45 |
{"text": prompt},
|
| 46 |
{"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
|
| 47 |
]
|
| 48 |
|
| 49 |
-
# Generate response with specific parameters
|
| 50 |
response = model.generate_content(
|
| 51 |
content_parts,
|
| 52 |
generation_config=generation_config,
|
|
@@ -58,42 +51,48 @@ def process_image(image):
|
|
| 58 |
]
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
# Pass the description to FLUX model
|
| 64 |
-
flux_interface = gr.load(
|
| 65 |
-
"models/black-forest-labs/FLUX.1-dev",
|
| 66 |
-
provider="together"
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Generate image based on the description
|
| 70 |
-
generated_image = flux_interface(description)
|
| 71 |
-
|
| 72 |
-
return description, generated_image
|
| 73 |
-
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Row():
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
submit_btn = gr.Button("Analyze and Generate")
|
| 87 |
-
|
| 88 |
-
with gr.Column():
|
| 89 |
-
output_text = gr.Textbox(lines=15, label="Image Description")
|
| 90 |
-
output_image = gr.Image(label="Generated Image")
|
| 91 |
|
|
|
|
| 92 |
submit_btn.click(
|
| 93 |
-
fn=
|
| 94 |
-
inputs=
|
| 95 |
-
outputs=[
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
# Initialize Gemini
|
| 7 |
+
GOOGLE_API_KEY = "AIzaSyBdz-qcLFRDsR-mm37AlRf2w6RZws2lDL0" # Replace with your actual API key
|
| 8 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 9 |
|
| 10 |
+
# Function to process the image with Gemini
|
| 11 |
def process_image(image):
|
| 12 |
try:
|
| 13 |
prompt = """
|
|
|
|
| 30 |
"max_output_tokens": 1024,
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Convert PIL Image to bytes
|
| 34 |
+
image_bytes = io.BytesIO()
|
| 35 |
+
image.save(image_bytes, format='PNG')
|
| 36 |
+
image_data = image_bytes.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
content_parts = [
|
| 39 |
{"text": prompt},
|
| 40 |
{"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
|
| 41 |
]
|
| 42 |
|
|
|
|
| 43 |
response = model.generate_content(
|
| 44 |
content_parts,
|
| 45 |
generation_config=generation_config,
|
|
|
|
| 51 |
]
|
| 52 |
)
|
| 53 |
|
| 54 |
+
return response.text
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"Error processing image: {str(e)}"
|
| 58 |
+
|
| 59 |
+
# Function to generate image from text using Hugging Face model
|
| 60 |
+
def generate_image_from_text(text):
|
| 61 |
+
try:
|
| 62 |
+
# Load the text-to-image model from Hugging Face Spaces
|
| 63 |
+
hf_model = gr.load("models/black-forest-labs/FLUX.1-dev", provider="together")
|
| 64 |
+
generated_image = hf_model(text) # Pass the text to the model
|
| 65 |
+
return generated_image
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error generating image: {str(e)}"
|
| 68 |
|
| 69 |
+
# Combined function to process the workflow
|
| 70 |
+
def full_workflow(image):
|
| 71 |
+
# Step 1: Get detailed analysis from Gemini
|
| 72 |
+
analysis_text = process_image(image)
|
| 73 |
+
|
| 74 |
+
# Step 2: Pass the analysis text to the text-to-image model
|
| 75 |
+
generated_image = generate_image_from_text(analysis_text)
|
| 76 |
|
| 77 |
+
# Return both the text analysis and the generated image
|
| 78 |
+
return analysis_text, generated_image
|
| 79 |
+
|
| 80 |
+
# Create Gradio interface
|
| 81 |
+
with gr.Blocks(title="Image Analysis and Regeneration") as demo:
|
| 82 |
+
gr.Markdown("### Upload an Image to Analyze and Regenerate")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 85 |
+
submit_btn = gr.Button("Analyze and Generate")
|
| 86 |
with gr.Row():
|
| 87 |
+
text_output = gr.Textbox(label="Gemini Analysis", lines=15)
|
| 88 |
+
image_output = gr.Image(label="Generated Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Connect the button to the full workflow
|
| 91 |
submit_btn.click(
|
| 92 |
+
fn=full_workflow,
|
| 93 |
+
inputs=image_input,
|
| 94 |
+
outputs=[text_output, image_output]
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
demo.launch(share=True)
|