Spaces:
Running
Running
Commit
·
c079997
1
Parent(s):
6962136
feat: working gradio_demo.py
Browse files- README.md +3 -1
- gradio_app.py → gradio_chat.py +0 -0
- gradio_demo.py +92 -0
README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
|
|
1 |
title: chat-image-editor
|
2 |
emoji: 💬
|
3 |
colorFrom: yellow
|
4 |
colorTo: purple
|
5 |
sdk: gradio
|
6 |
sdk_version: 5.16.1
|
7 |
-
app_file:
|
8 |
pinned: false
|
|
|
|
1 |
+
---
|
2 |
title: chat-image-editor
|
3 |
emoji: 💬
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.16.1
|
8 |
+
app_file: gradio_demo.py
|
9 |
pinned: false
|
10 |
+
---
|
gradio_app.py → gradio_chat.py
RENAMED
File without changes
|
gradio_demo.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from src.agents.mask_generation_agent import mask_generation_agent, ImageEditDeps
|
3 |
+
import os
|
4 |
+
from src.hopter.client import Hopter, Environment
|
5 |
+
from src.services.generate_mask import GenerateMaskService
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from src.utils import image_path_to_uri
|
8 |
+
from pydantic_ai.messages import (
|
9 |
+
ToolCallPart,
|
10 |
+
ToolReturnPart
|
11 |
+
)
|
12 |
+
from src.agents.mask_generation_agent import EditImageResult
|
13 |
+
from pydantic_ai.agent import Agent
|
14 |
+
from pydantic_ai.models.openai import OpenAIModel
|
15 |
+
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
async def process_edit(image, instruction):
|
19 |
+
hopter = Hopter(os.environ.get("HOPTER_API_KEY"), environment=Environment.STAGING)
|
20 |
+
mask_service = GenerateMaskService(hopter=hopter)
|
21 |
+
messages = [
|
22 |
+
{
|
23 |
+
"type": "text",
|
24 |
+
"text": instruction
|
25 |
+
},
|
26 |
+
]
|
27 |
+
if image:
|
28 |
+
messages.append(
|
29 |
+
{"type": "image_url", "image_url": {"url": image_path_to_uri(image)}}
|
30 |
+
)
|
31 |
+
deps = ImageEditDeps(
|
32 |
+
edit_instruction=instruction,
|
33 |
+
image_url=image_path_to_uri(image),
|
34 |
+
hopter_client=hopter,
|
35 |
+
mask_service=mask_service
|
36 |
+
)
|
37 |
+
result = await mask_generation_agent.run(
|
38 |
+
messages,
|
39 |
+
deps=deps
|
40 |
+
)
|
41 |
+
# Extract the edited image URL from the tool return
|
42 |
+
for message in result.new_messages():
|
43 |
+
for part in message.parts:
|
44 |
+
if isinstance(part, ToolReturnPart) and isinstance(part.content, EditImageResult):
|
45 |
+
return part.content.edited_image_url
|
46 |
+
return None
|
47 |
+
|
48 |
+
async def use_edited_image(edited_image):
|
49 |
+
return edited_image
|
50 |
+
|
51 |
+
# Create the Gradio interface
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("# PicEdit")
|
54 |
+
gr.Markdown("""
|
55 |
+
Welcome to PicEdit - an AI-powered image editing tool.
|
56 |
+
Simply upload an image and describe the changes you want to make in natural language.
|
57 |
+
""")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
# Input image on the left
|
61 |
+
input_image = gr.Image(label="Original Image", type="filepath")
|
62 |
+
# Output image on the right
|
63 |
+
with gr.Column():
|
64 |
+
output_image = gr.Image(label="Edited Image", type="filepath")
|
65 |
+
use_edited_btn = gr.Button("Use Edited Image")
|
66 |
+
|
67 |
+
# Text input for editing instructions
|
68 |
+
instruction = gr.Textbox(
|
69 |
+
label="Editing Instructions",
|
70 |
+
placeholder="Describe the changes you want to make to the image..."
|
71 |
+
)
|
72 |
+
|
73 |
+
# Submit button
|
74 |
+
submit_btn = gr.Button("Apply Edit")
|
75 |
+
|
76 |
+
# Set up the event handlers
|
77 |
+
submit_btn.click(
|
78 |
+
fn=process_edit,
|
79 |
+
inputs=[input_image, instruction],
|
80 |
+
outputs=output_image
|
81 |
+
)
|
82 |
+
|
83 |
+
use_edited_btn.click(
|
84 |
+
fn=use_edited_image,
|
85 |
+
inputs=[output_image],
|
86 |
+
outputs=[input_image]
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
demo.launch()
|
91 |
+
|
92 |
+
|