Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -70,34 +70,61 @@ def generate_image(prompt, output_filename="generated_image"):
|
|
70 |
|
71 |
return None, "No image generated"
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
with gr.Row():
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
95 |
|
96 |
-
#
|
97 |
-
|
98 |
-
fn=
|
99 |
-
inputs=[prompt_input,
|
100 |
-
outputs=[
|
101 |
)
|
102 |
|
103 |
if __name__ == "__main__":
|
|
|
70 |
|
71 |
return None, "No image generated"
|
72 |
|
73 |
+
# Function to handle chat interaction
|
74 |
+
def chat_handler(user_input, chat_history):
|
75 |
+
# Add user message to chat history
|
76 |
+
chat_history.append([user_input, None])
|
77 |
|
78 |
+
# Generate image based on user input
|
79 |
+
img, status = generate_image(user_input)
|
80 |
+
|
81 |
+
# Add AI response to chat history
|
82 |
+
if img:
|
83 |
+
chat_history.append([None, img])
|
84 |
+
chat_history.append([None, status])
|
85 |
+
|
86 |
+
return chat_history, ""
|
87 |
+
|
88 |
+
# Create Gradio interface with chatbot layout
|
89 |
+
with gr.Blocks(title="Image Editing Chatbot") as demo:
|
90 |
+
gr.Markdown("# Image Editing Chatbot")
|
91 |
+
gr.Markdown("Type a prompt to generate or edit an image using Google's Gemini model")
|
92 |
+
|
93 |
+
# Chatbot display area
|
94 |
+
chatbot = gr.Chatbot(
|
95 |
+
label="Chat",
|
96 |
+
height=400,
|
97 |
+
bubble_full_width=False,
|
98 |
+
avatar_images=(None, None) # No avatars for simplicity
|
99 |
+
)
|
100 |
+
|
101 |
+
# Input area
|
102 |
with gr.Row():
|
103 |
+
prompt_input = gr.Textbox(
|
104 |
+
label="",
|
105 |
+
placeholder="Type something",
|
106 |
+
show_label=False,
|
107 |
+
container=False,
|
108 |
+
scale=4
|
109 |
+
)
|
110 |
+
run_btn = gr.Button("Run", scale=1)
|
111 |
+
|
112 |
+
# State to maintain chat history
|
113 |
+
chat_state = gr.State([])
|
114 |
+
|
115 |
+
# Connect the button to the chat handler
|
116 |
+
run_btn.click(
|
117 |
+
fn=chat_handler,
|
118 |
+
inputs=[prompt_input, chat_state],
|
119 |
+
outputs=[chatbot, prompt_input],
|
120 |
+
_js="() => {return [document.querySelector('input').value, null]}"
|
121 |
+
)
|
122 |
|
123 |
+
# Also allow Enter key to submit
|
124 |
+
prompt_input.submit(
|
125 |
+
fn=chat_handler,
|
126 |
+
inputs=[prompt_input, chat_state],
|
127 |
+
outputs=[chatbot, prompt_input]
|
128 |
)
|
129 |
|
130 |
if __name__ == "__main__":
|