Upload 3 files
Browse files- README.md +7 -7
- app.py +193 -0
- requirements .txt +6 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.21.0
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
-
license:
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Gemini Multi Ai
|
3 |
+
emoji: π
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.21.0
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import uuid
|
5 |
+
import tempfile
|
6 |
+
from PIL import Image
|
7 |
+
import gradio as gr
|
8 |
+
import base64
|
9 |
+
import mimetypes
|
10 |
+
import logging
|
11 |
+
|
12 |
+
from google import genai
|
13 |
+
from google.genai import types
|
14 |
+
|
15 |
+
# .env νμΌμ μ μ₯λ νκ²½λ³μ λ‘λ (python-dotenv μ€μΉ νμ: pip install python-dotenv)
|
16 |
+
from dotenv import load_dotenv
|
17 |
+
load_dotenv()
|
18 |
+
|
19 |
+
# λ‘κΉ
μ€μ (λ‘κ·Έ λ 벨: DEBUG)
|
20 |
+
logging.basicConfig(level=logging.DEBUG,
|
21 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
22 |
+
logger = logging.getLogger(__name__)
|
23 |
+
|
24 |
+
|
25 |
+
def save_binary_file(file_name, data):
|
26 |
+
logger.debug(f"νμΌμ μ΄μ§ λ°μ΄ν° μ μ₯ μ€: {file_name}")
|
27 |
+
with open(file_name, "wb") as f:
|
28 |
+
f.write(data)
|
29 |
+
logger.debug(f"νμΌ μ μ₯ μλ£: {file_name}")
|
30 |
+
|
31 |
+
|
32 |
+
def generate(text, file_name, model="gemini-2.0-flash-exp-image-generation"):
|
33 |
+
logger.debug(f"generate ν¨μ μμ - ν
μ€νΈ: '{text}', νμΌλͺ
: '{file_name}', λͺ¨λΈ: '{model}'")
|
34 |
+
|
35 |
+
try:
|
36 |
+
# API ν€λ νκ²½λ³μμμ λΆλ¬μ΄
|
37 |
+
effective_api_key = os.environ.get("GEMINI_API_KEY")
|
38 |
+
if effective_api_key:
|
39 |
+
logger.debug("νκ²½λ³μμμ API ν€ λΆλ¬μ΄")
|
40 |
+
else:
|
41 |
+
logger.error("API ν€κ° νκ²½λ³μμ μ€μ λμ§ μμμ΅λλ€.")
|
42 |
+
raise ValueError("API ν€κ° νμν©λλ€.")
|
43 |
+
|
44 |
+
client = genai.Client(api_key=effective_api_key)
|
45 |
+
logger.debug("Gemini ν΄λΌμ΄μΈνΈ μ΄κΈ°ν μλ£.")
|
46 |
+
|
47 |
+
# νμΌ μ
λ‘λ
|
48 |
+
files = [
|
49 |
+
client.files.upload(file=file_name),
|
50 |
+
]
|
51 |
+
logger.debug(f"νμΌ μ
λ‘λ μλ£. URI: {files[0].uri}, MIME νμ
: {files[0].mime_type}")
|
52 |
+
|
53 |
+
# 컨ν
μΈ κ°μ²΄ μμ±: νμΌ URIμ ν
μ€νΈ ν둬ννΈλ₯Ό ν¨κ» ν¬ν¨
|
54 |
+
contents = [
|
55 |
+
types.Content(
|
56 |
+
role="user",
|
57 |
+
parts=[
|
58 |
+
types.Part.from_uri(
|
59 |
+
file_uri=files[0].uri,
|
60 |
+
mime_type=files[0].mime_type,
|
61 |
+
),
|
62 |
+
types.Part.from_text(text=text),
|
63 |
+
],
|
64 |
+
),
|
65 |
+
]
|
66 |
+
logger.debug(f"컨ν
μΈ κ°μ²΄ μμ± μλ£: {contents}")
|
67 |
+
|
68 |
+
generate_content_config = types.GenerateContentConfig(
|
69 |
+
temperature=1,
|
70 |
+
top_p=0.95,
|
71 |
+
top_k=40,
|
72 |
+
max_output_tokens=8192,
|
73 |
+
response_modalities=[
|
74 |
+
"image",
|
75 |
+
"text",
|
76 |
+
],
|
77 |
+
response_mime_type="text/plain",
|
78 |
+
)
|
79 |
+
logger.debug(f"μμ± μ€μ : {generate_content_config}")
|
80 |
+
|
81 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
82 |
+
temp_path = tmp.name
|
83 |
+
logger.debug(f"μμ νμΌ μμ±λ¨: {temp_path}")
|
84 |
+
|
85 |
+
response_stream = client.models.generate_content_stream(
|
86 |
+
model=model,
|
87 |
+
contents=contents,
|
88 |
+
config=generate_content_config,
|
89 |
+
)
|
90 |
+
|
91 |
+
logger.debug("μλ΅ μ€νΈλ¦Ό μ²λ¦¬ μμ...")
|
92 |
+
for chunk in response_stream:
|
93 |
+
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
94 |
+
logger.warning("chunkμ ν보, 컨ν
μΈ , λλ ννΈκ° μμ΅λλ€. 건λλλλ€.")
|
95 |
+
continue
|
96 |
+
|
97 |
+
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
98 |
+
if inline_data:
|
99 |
+
save_binary_file(temp_path, inline_data.data)
|
100 |
+
logger.info(f"MIME νμ
{inline_data.mime_type}μ νμΌμ΄ μ μ₯λ¨: {temp_path} (ν둬ννΈ: {text})")
|
101 |
+
else:
|
102 |
+
logger.info(f"μμ λ ν
μ€νΈ: {chunk.text}")
|
103 |
+
print(chunk.text)
|
104 |
+
|
105 |
+
logger.debug(f"Raw chunk: {chunk}")
|
106 |
+
|
107 |
+
del files
|
108 |
+
logger.debug("μ
λ‘λλ νμΌ μ 보 μμ μλ£.")
|
109 |
+
return temp_path
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
logger.exception("μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ:")
|
113 |
+
return None # μ€λ₯ λ°μ μ None λ°ν
|
114 |
+
|
115 |
+
|
116 |
+
def process_image_and_prompt(composite_pil, prompt):
|
117 |
+
logger.debug(f"process_image_and_prompt ν¨μ μμ - ν둬ννΈ: '{prompt}'")
|
118 |
+
try:
|
119 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
120 |
+
composite_path = tmp.name
|
121 |
+
composite_pil.save(composite_path)
|
122 |
+
logger.debug(f"ν©μ± μ΄λ―Έμ§ μ μ₯ μλ£: {composite_path}")
|
123 |
+
|
124 |
+
file_name = composite_path
|
125 |
+
input_text = prompt
|
126 |
+
model = "gemini-2.0-flash-exp-image-generation"
|
127 |
+
|
128 |
+
gemma_edited_image_path = generate(text=input_text, file_name=file_name, model=model)
|
129 |
+
|
130 |
+
if gemma_edited_image_path:
|
131 |
+
logger.debug(f"μ΄λ―Έμ§ μμ± μλ£. κ²½λ‘: {gemma_edited_image_path}")
|
132 |
+
result_img = Image.open(gemma_edited_image_path)
|
133 |
+
if result_img.mode == "RGBA":
|
134 |
+
result_img = result_img.convert("RGB")
|
135 |
+
return [result_img]
|
136 |
+
else:
|
137 |
+
logger.error("generate ν¨μμμ None λ°νλ¨.")
|
138 |
+
return [] # μ€λ₯ μ λΉ λ¦¬μ€νΈ λ°ν
|
139 |
+
|
140 |
+
except Exception as e:
|
141 |
+
logger.exception("process_image_and_prompt ν¨μμμ μ€λ₯ λ°μ:")
|
142 |
+
return [] # μ€λ₯ μ λΉ λ¦¬μ€νΈ λ°ν
|
143 |
+
|
144 |
+
|
145 |
+
# --- Gradio μΈν°νμ΄μ€ κ΅¬μ± ---
|
146 |
+
with gr.Blocks() as demo:
|
147 |
+
gr.HTML(
|
148 |
+
"""
|
149 |
+
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
|
150 |
+
<div style="background-color: var(--block-background-fill); border-radius: 8px">
|
151 |
+
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
|
152 |
+
</div>
|
153 |
+
<div>
|
154 |
+
<h1>Geminiλ₯Ό μ΄μ©ν μ΄λ―Έμ§ νΈμ§</h1>
|
155 |
+
<p>Gemini API ν€λ νκ²½λ³μ(GEMINI_API_KEY)λ‘ μ€μ λμ΄ μμ΅λλ€.</p>
|
156 |
+
</div>
|
157 |
+
</div>
|
158 |
+
"""
|
159 |
+
)
|
160 |
+
gr.Markdown("μ΄λ―Έμ§λ₯Ό μ
λ‘λνκ³ , νΈμ§ν λ΄μ©μ μ
λ ₯νμΈμ.")
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
with gr.Column():
|
164 |
+
image_input = gr.Image(type="pil", label="μ΄λ―Έμ§ μ
λ‘λ", image_mode="RGBA")
|
165 |
+
prompt_input = gr.Textbox(
|
166 |
+
lines=2,
|
167 |
+
placeholder="νΈμ§ν λ΄μ©μ μ
λ ₯νμΈμ...",
|
168 |
+
label="νΈμ§ ν둬ννΈ"
|
169 |
+
)
|
170 |
+
submit_btn = gr.Button("μ΄λ―Έμ§ νΈμ§ μ€ν")
|
171 |
+
with gr.Column():
|
172 |
+
output_gallery = gr.Gallery(label="νΈμ§ κ²°κ³Ό")
|
173 |
+
|
174 |
+
submit_btn.click(
|
175 |
+
fn=process_image_and_prompt,
|
176 |
+
inputs=[image_input, prompt_input],
|
177 |
+
outputs=output_gallery,
|
178 |
+
)
|
179 |
+
|
180 |
+
# --- ν
μ€νΈ μ½λ ---
|
181 |
+
# ν
μ€νΈμ© λλ―Έ μ΄λ―Έμ§ (μ€μ μ΄λ―Έμ§λ‘ λ체 κ°λ₯)
|
182 |
+
dummy_image = Image.new("RGBA", (100, 100), color="red")
|
183 |
+
dummy_prompt = "μ΄λ―Έμ§λ₯Ό νλμμΌλ‘ λ³κ²½ν΄μ€"
|
184 |
+
|
185 |
+
logger.info("process_image_and_prompt ν¨μλ₯Ό μ§μ νΈμΆν©λλ€...")
|
186 |
+
result = process_image_and_prompt(dummy_image, dummy_prompt)
|
187 |
+
|
188 |
+
if result:
|
189 |
+
logger.info(f"μ§μ νΈμΆ μ±κ³΅. κ²°κ³Ό: {result}")
|
190 |
+
else:
|
191 |
+
logger.error("μ§μ νΈμΆ μ€ν¨.")
|
192 |
+
|
193 |
+
demo.launch(share=True)
|
requirements .txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
google-genai==1.5.0
|
2 |
+
gradio
|
3 |
+
pydantic==2.10.6
|
4 |
+
google-generativeai
|
5 |
+
pillow
|
6 |
+
dotenv
|