Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,32 +65,28 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 65 |
genai.configure(api_key=api_key)
|
| 66 |
|
| 67 |
# ์ด๋ฏธ์ง ํ์ผ ์
๋ก๋
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
# API์ ์ ๋ฌํ content ๊ตฌ์ฑ
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
file_uri=uploaded_file.uri,
|
| 78 |
-
mime_type=uploaded_file.mime_type,
|
| 79 |
-
),
|
| 80 |
-
# ์ด์ด์ text ์ง์์ฌํญ์ ํฌํจ
|
| 81 |
-
genai_types.Part.from_text(text=text),
|
| 82 |
-
],
|
| 83 |
-
),
|
| 84 |
]
|
| 85 |
-
|
| 86 |
# ์์ฑ(ํธ์ง) ์ค์
|
| 87 |
-
generation_config =
|
| 88 |
-
temperature
|
| 89 |
-
top_p
|
| 90 |
-
top_k
|
| 91 |
-
max_output_tokens
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
|
| 95 |
text_response = "" # API๊ฐ ๋ฐํํ ํ
์คํธ ๋์
|
| 96 |
image_path = None # API๊ฐ ๋ฐํํ ์ด๋ฏธ์ง ํ์ผ์ ๋ก์ปฌ ๊ฒฝ๋ก
|
|
@@ -99,34 +95,28 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 99 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 100 |
temp_path = tmp.name
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# ์คํธ๋ฆฌ๋ฐ๋ chunk๋ค์์ ์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ์ถ
|
| 111 |
-
for chunk in response:
|
| 112 |
-
for candidate in chunk.candidates:
|
| 113 |
-
for part in candidate.content.parts:
|
| 114 |
-
# ์ด๋ฏธ์ง์ธ ๊ฒฝ์ฐ
|
| 115 |
-
if hasattr(part, 'inline_data') and part.inline_data:
|
| 116 |
-
save_binary_file(temp_path, part.inline_data.data)
|
| 117 |
-
image_path = temp_path
|
| 118 |
-
break
|
| 119 |
-
# ํ
์คํธ์ธ ๊ฒฝ์ฐ
|
| 120 |
-
elif hasattr(part, 'text'):
|
| 121 |
-
text_response += part.text + "\n"
|
| 122 |
-
|
| 123 |
-
if image_path:
|
| 124 |
-
break
|
| 125 |
-
if image_path:
|
| 126 |
-
break
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
return image_path, text_response
|
| 132 |
|
|
|
|
| 65 |
genai.configure(api_key=api_key)
|
| 66 |
|
| 67 |
# ์ด๋ฏธ์ง ํ์ผ ์
๋ก๋
|
| 68 |
+
with open(file_name, "rb") as f:
|
| 69 |
+
image_data = f.read()
|
| 70 |
+
|
| 71 |
+
# ์ต์ Gemini API ๊ตฌ์กฐ์ ๋ง์ถฐ ์์ฒญ ๊ตฌ์ฑ
|
| 72 |
+
model = genai.GenerativeModel(model)
|
| 73 |
+
|
| 74 |
# API์ ์ ๋ฌํ content ๊ตฌ์ฑ
|
| 75 |
+
image_parts = [
|
| 76 |
+
{
|
| 77 |
+
"mime_type": "image/png",
|
| 78 |
+
"data": image_data
|
| 79 |
+
},
|
| 80 |
+
text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
]
|
| 82 |
+
|
| 83 |
# ์์ฑ(ํธ์ง) ์ค์
|
| 84 |
+
generation_config = {
|
| 85 |
+
"temperature": 1,
|
| 86 |
+
"top_p": 0.95,
|
| 87 |
+
"top_k": 40,
|
| 88 |
+
"max_output_tokens": 8192,
|
| 89 |
+
}
|
|
|
|
| 90 |
|
| 91 |
text_response = "" # API๊ฐ ๋ฐํํ ํ
์คํธ ๋์
|
| 92 |
image_path = None # API๊ฐ ๋ฐํํ ์ด๋ฏธ์ง ํ์ผ์ ๋ก์ปฌ ๊ฒฝ๋ก
|
|
|
|
| 95 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 96 |
temp_path = tmp.name
|
| 97 |
|
| 98 |
+
try:
|
| 99 |
+
# ์คํธ๋ฆฌ๋ฐ ํํ๋ก ์๋ต์ ๋ฐ์
|
| 100 |
+
response = model.generate_content(
|
| 101 |
+
contents=image_parts,
|
| 102 |
+
generation_config=generation_config,
|
| 103 |
+
stream=True
|
| 104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# ์คํธ๋ฆฌ๋ฐ๋ chunk๋ค์์ ์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ์ถ
|
| 107 |
+
for chunk in response:
|
| 108 |
+
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ๊ฐ ์๋์ง ํ์ธ
|
| 109 |
+
if hasattr(chunk, 'parts'):
|
| 110 |
+
for part in chunk.parts:
|
| 111 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
| 112 |
+
save_binary_file(temp_path, part.inline_data.data)
|
| 113 |
+
image_path = temp_path
|
| 114 |
+
elif hasattr(part, 'text'):
|
| 115 |
+
text_response += part.text
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
logging.error(f"Gemini API error: {e}")
|
| 119 |
+
return None, str(e)
|
| 120 |
|
| 121 |
return image_path, text_response
|
| 122 |
|