Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,101 +0,0 @@
|
|
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 of gender, race, or any other demographic diversity, look stylish."
|
47 |
-
model_name = 'gemini-pro-vision' if image_prompt else 'gemini-flash-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", width=200)
|
75 |
-
text_prompt_component = gr.Textbox(
|
76 |
-
placeholder="Describe your style needs",
|
77 |
-
label="Enter your prompt",
|
78 |
-
width=300
|
79 |
-
)
|
80 |
-
run_button_component = gr.Button("Ask Stylist")
|
81 |
-
|
82 |
-
with gr.Blocks() as demo:
|
83 |
-
gr.HTML(TITLE)
|
84 |
-
gr.HTML(SUBTITLE)
|
85 |
-
gr.HTML(DUPLICATE)
|
86 |
-
with gr.Row():
|
87 |
-
google_key_component.render()
|
88 |
-
with gr.Row():
|
89 |
-
text_prompt_component.render()
|
90 |
-
image_prompt_component.render()
|
91 |
-
chatbot_component = gr.Chatbot(label='Gemini Personal Stylist')
|
92 |
-
chatbot_component.render()
|
93 |
-
run_button_component.render()
|
94 |
-
|
95 |
-
run_button_component.click(
|
96 |
-
inputs=[text_prompt_component, image_prompt_component, chatbot_component],
|
97 |
-
outputs=chatbot_component,
|
98 |
-
fn=bot
|
99 |
-
)
|
100 |
-
|
101 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|