Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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">💅 Get ready with your personal stylist</h2>"""
|
15 |
+
DUPLICATE = """
|
16 |
+
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
|
17 |
+
<a href="https://huggingface.co/spaces/Rahatara/build_with_gemini/blob/main/allgemapp.py?duplicate=true">
|
18 |
+
<img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
|
19 |
+
</a>
|
20 |
+
<span>Duplicate the Space and run securely with your
|
21 |
+
<a href="https://makersuite.google.com/app/apikey">GOOGLE API KEY</a>.
|
22 |
+
</span>
|
23 |
+
</div>
|
24 |
+
"""
|
25 |
+
|
26 |
+
IMAGE_WIDTH = 512
|
27 |
+
|
28 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
29 |
+
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
30 |
+
return image.resize((IMAGE_WIDTH, image_height))
|
31 |
+
|
32 |
+
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
33 |
+
return "", chatbot + [[text_prompt, None]]
|
34 |
+
|
35 |
+
def bot(
|
36 |
+
google_key: str,
|
37 |
+
image_prompt: Optional[Image.Image],
|
38 |
+
text_prompt: str,
|
39 |
+
chatbot: List[Tuple[str, str]]
|
40 |
+
):
|
41 |
+
google_key = google_key or GOOGLE_API_KEY
|
42 |
+
if not google_key:
|
43 |
+
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
44 |
+
|
45 |
+
genai.configure(api_key=google_key)
|
46 |
+
instructions = "You are an expert stylist. You suggest and evaluate any style related question. To make anyone regardless gender, race or any other demographic diversity, look stylish."
|
47 |
+
model_name = 'gemini-pro-vision' if image_prompt else 'gemini-1.5-pro-latest'
|
48 |
+
|
49 |
+
if image_prompt:
|
50 |
+
images_with_prompt = [text_prompt, instructions, preprocess_image(image_prompt)]
|
51 |
+
model = genai.GenerativeModel(model_name)
|
52 |
+
response = model.generate_content(images_with_prompt, stream=True)
|
53 |
+
else:
|
54 |
+
text_with_prompt = [text_prompt, instructions]
|
55 |
+
model = genai.GenerativeModel(model_name)
|
56 |
+
response = model.generate_content(text_with_prompt, stream=True)
|
57 |
+
|
58 |
+
response.resolve()
|
59 |
+
|
60 |
+
chatbot[-1][1] = ""
|
61 |
+
for chunk in response:
|
62 |
+
for i in range(0, len(chunk.text), 10):
|
63 |
+
chatbot[-1][1] += chunk.text[i:i + 10]
|
64 |
+
time.sleep(0.01)
|
65 |
+
yield chatbot
|
66 |
+
|
67 |
+
google_key_component = gr.Textbox(
|
68 |
+
label="GOOGLE API KEY",
|
69 |
+
type="password",
|
70 |
+
placeholder="Enter your Google API Key",
|
71 |
+
visible=GOOGLE_API_KEY is None
|
72 |
+
)
|
73 |
+
|
74 |
+
image_prompt_component = gr.Image(type="pil", label="Upload Image")
|
75 |
+
chatbot_component = gr.Chatbot(label='Gemini Personal Stylist')
|
76 |
+
text_prompt_component = gr.Textbox(
|
77 |
+
placeholder="Describe your style needs",
|
78 |
+
label="Enter your prompt and press Enter"
|
79 |
+
)
|
80 |
+
run_button_component = gr.Button("Ask Stylist")
|
81 |
+
|
82 |
+
user_inputs = [text_prompt_component, chatbot_component]
|
83 |
+
bot_inputs = [google_key_component, image_prompt_component, text_prompt_component, chatbot_component]
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
gr.HTML(TITLE)
|
87 |
+
gr.HTML(SUBTITLE)
|
88 |
+
gr.HTML(DUPLICATE)
|
89 |
+
with gr.Row():
|
90 |
+
google_key_component.render()
|
91 |
+
image_prompt_component.render()
|
92 |
+
text_prompt_component.render()
|
93 |
+
chatbot_component.render()
|
94 |
+
run_button_component.render()
|
95 |
+
|
96 |
+
run_button_component.click(
|
97 |
+
fn=user,
|
98 |
+
inputs=user_inputs,
|
99 |
+
outputs=[text_prompt_component, chatbot_component],
|
100 |
+
queue=False
|
101 |
+
).then(
|
102 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
|
103 |
+
)
|
104 |
+
|
105 |
+
text_prompt_component.submit(
|
106 |
+
fn=user,
|
107 |
+
inputs=user_inputs,
|
108 |
+
outputs=[text_prompt_component, chatbot_component],
|
109 |
+
queue=False
|
110 |
+
).then(
|
111 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
|
112 |
+
)
|
113 |
+
|
114 |
+
demo.launch()
|