Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from typing import List, Tuple, Optional
|
| 4 |
+
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
print("google-generativeai:", genai.__version__)
|
| 10 |
+
|
| 11 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 12 |
+
|
| 13 |
+
TITLE = """<h1 align="center">👗 Gemini Personal Stylist Chatbot 🛍️</h1>"""
|
| 14 |
+
SUBTITLE = """<h2 align="center">Chat with Your Personal Stylist using Gemini Vision Pro 🖼️</h2>"""
|
| 15 |
+
|
| 16 |
+
IMAGE_WIDTH = 512
|
| 17 |
+
|
| 18 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 19 |
+
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 20 |
+
return image.resize((IMAGE_WIDTH, image_height))
|
| 21 |
+
|
| 22 |
+
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
| 23 |
+
return "", chatbot + [[text_prompt, None]]
|
| 24 |
+
|
| 25 |
+
def bot(
|
| 26 |
+
google_key: str,
|
| 27 |
+
image_prompt: Optional[Image.Image],
|
| 28 |
+
text_prompt: str,
|
| 29 |
+
chatbot: List[Tuple[str, str]]
|
| 30 |
+
):
|
| 31 |
+
google_key = google_key or GOOGLE_API_KEY
|
| 32 |
+
if not google_key:
|
| 33 |
+
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 34 |
+
|
| 35 |
+
genai.configure(api_key=google_key)
|
| 36 |
+
instructions = "Instructions: Consider the following image and provide styling advice:"
|
| 37 |
+
images_with_prompt = [text_prompt, instructions] + [preprocess_image(image_prompt)]
|
| 38 |
+
|
| 39 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 40 |
+
response = model.generate_content(images_with_prompt, stream=True)
|
| 41 |
+
response.resolve()
|
| 42 |
+
|
| 43 |
+
chatbot[-1][1] = ""
|
| 44 |
+
for chunk in response:
|
| 45 |
+
for i in range(0, len(chunk.text), 10):
|
| 46 |
+
chatbot[-1][1] += chunk.text[i:i + 10]
|
| 47 |
+
time.sleep(0.01)
|
| 48 |
+
yield chatbot
|
| 49 |
+
|
| 50 |
+
google_key_component = gr.Textbox(
|
| 51 |
+
label="GOOGLE API KEY",
|
| 52 |
+
type="password",
|
| 53 |
+
placeholder="Enter your Google API Key",
|
| 54 |
+
visible=GOOGLE_API_KEY is None
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
image_prompt_component = gr.Image(type="pil", label="Upload Image")
|
| 58 |
+
chatbot_component = gr.Chatbot(label='Gemini Personal Stylist')
|
| 59 |
+
text_prompt_component = gr.Textbox(
|
| 60 |
+
placeholder="Describe your style needs",
|
| 61 |
+
label="Enter your prompt and press Enter"
|
| 62 |
+
)
|
| 63 |
+
run_button_component = gr.Button("Ask Stylist")
|
| 64 |
+
|
| 65 |
+
user_inputs = [text_prompt_component, chatbot_component]
|
| 66 |
+
bot_inputs = [google_key_component, image_prompt_component, text_prompt_component, chatbot_component]
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.HTML(TITLE)
|
| 70 |
+
gr.HTML(SUBTITLE)
|
| 71 |
+
with gr.Column():
|
| 72 |
+
google_key_component.render()
|
| 73 |
+
image_prompt_component.render()
|
| 74 |
+
chatbot_component.render()
|
| 75 |
+
text_prompt_component.render()
|
| 76 |
+
run_button_component.render()
|
| 77 |
+
|
| 78 |
+
run_button_component.click(
|
| 79 |
+
fn=user,
|
| 80 |
+
inputs=user_inputs,
|
| 81 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 82 |
+
queue=False
|
| 83 |
+
).then(
|
| 84 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
text_prompt_component.submit(
|
| 88 |
+
fn=user,
|
| 89 |
+
inputs=user_inputs,
|
| 90 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 91 |
+
queue=False
|
| 92 |
+
).then(
|
| 93 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|