Update app.py
Browse files
app.py
CHANGED
@@ -18,53 +18,52 @@ load_dotenv()
|
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
-
# LLM
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
return None
|
27 |
-
|
28 |
-
# genai ํด๋ผ์ด์ธํธ ์์ฑ
|
29 |
-
client = genai.Client(api_key=api_key)
|
30 |
-
|
31 |
-
# GenerativeModel ์ธ์คํด์ค ์์ฑ
|
32 |
-
model = client.models.get_model("gemini-2.0-flash")
|
33 |
-
|
34 |
-
return model
|
35 |
-
|
36 |
-
# LLM ๋ชจ๋ธ๋ก ํ๊ตญ์ด ํ๋กฌํํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
37 |
-
def translate_to_english(korean_prompt, model):
|
38 |
-
if not korean_prompt or not model:
|
39 |
-
return korean_prompt
|
40 |
-
|
41 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
translation_prompt = f"""
|
43 |
-
|
44 |
-
๋ฒ์ญํ ํ
์คํธ: {korean_prompt}
|
45 |
|
46 |
-
|
|
|
|
|
47 |
"""
|
48 |
|
49 |
-
#
|
50 |
-
response =
|
51 |
-
types.GenerateContentRequest(
|
52 |
-
contents=[types.Content(parts=[types.Part(text=translation_prompt)])],
|
53 |
-
generation_config=types.GenerationConfig(
|
54 |
-
temperature=0.2,
|
55 |
-
max_output_tokens=1024,
|
56 |
-
top_p=0.9,
|
57 |
-
)
|
58 |
-
)
|
59 |
-
)
|
60 |
|
61 |
# ์๋ต์์ ํ
์คํธ ์ถ์ถ
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
except Exception as e:
|
66 |
logger.exception(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
67 |
-
return
|
68 |
|
69 |
def save_binary_file(file_name, data):
|
70 |
with open(file_name, "wb") as f:
|
@@ -134,7 +133,7 @@ def preprocess_prompt(prompt, image1=None, image2=None, image3=None):
|
|
134 |
|
135 |
return prompt
|
136 |
|
137 |
-
def generate_with_images(prompt, images
|
138 |
"""
|
139 |
๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ ์ฌ๋ฐ๋ฅธ API ํธ์ถ ๋ฐฉ์ ๊ตฌํ
|
140 |
์ฌ์๋ ๋ก์ง ์ถ๊ฐ
|
@@ -153,7 +152,7 @@ def generate_with_images(prompt, images, gemini_model):
|
|
153 |
client = genai.Client(api_key=api_key)
|
154 |
|
155 |
# ํ๋กฌํํธ ๋ฒ์ญ
|
156 |
-
english_prompt =
|
157 |
logger.info(f"์๋ณธ ํ๋กฌํํธ: {prompt}")
|
158 |
logger.info(f"๋ฒ์ญ๋ ํ๋กฌํํธ: {english_prompt}")
|
159 |
|
@@ -222,7 +221,7 @@ def generate_with_images(prompt, images, gemini_model):
|
|
222 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
223 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
224 |
|
225 |
-
def process_images_with_prompt(image1, image2, image3, prompt
|
226 |
"""
|
227 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
228 |
"""
|
@@ -250,7 +249,7 @@ def process_images_with_prompt(image1, image2, image3, prompt, gemini_model):
|
|
250 |
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
251 |
|
252 |
# ์ด๋ฏธ์ง ์์ฑ API ํธ์ถ
|
253 |
-
return generate_with_images(prompt, images
|
254 |
|
255 |
except Exception as e:
|
256 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
@@ -272,9 +271,6 @@ def update_prompt_from_function(function_choice):
|
|
272 |
|
273 |
# Gradio ์ธํฐํ์ด์ค (์์ ๋ ๋ฒ์ )
|
274 |
def create_interface():
|
275 |
-
# Gemini ๋ชจ๋ธ ์ด๊ธฐํ
|
276 |
-
gemini_model = initialize_gemini()
|
277 |
-
|
278 |
with gr.Blocks() as demo:
|
279 |
gr.HTML(
|
280 |
"""
|
@@ -338,7 +334,7 @@ def create_interface():
|
|
338 |
def process_and_show_prompt(image1, image2, image3, prompt):
|
339 |
try:
|
340 |
# ์ด๋ฏธ์ง ์์ฑ ํจ์ ํธ์ถ
|
341 |
-
result_img, status = process_images_with_prompt(image1, image2, image3, prompt
|
342 |
|
343 |
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ
|
344 |
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
|
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
+
# LLM ์ค์ ๋ฐ ๋ฒ์ญ ํจ์
|
22 |
+
def get_translation(korean_text):
|
23 |
+
"""
|
24 |
+
ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
25 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
try:
|
27 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
28 |
+
if not api_key:
|
29 |
+
logger.error("GEMINI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
30 |
+
return korean_text
|
31 |
+
|
32 |
+
# ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
33 |
+
client = genai.GenerativeModel(
|
34 |
+
model_name="gemini-2.0-flash",
|
35 |
+
generation_config={
|
36 |
+
"temperature": 0.2,
|
37 |
+
"max_output_tokens": 1024,
|
38 |
+
"top_p": 0.9,
|
39 |
+
},
|
40 |
+
system_instruction="You are a professional translator who translates Korean to English accurately.",
|
41 |
+
api_key=api_key
|
42 |
+
)
|
43 |
+
|
44 |
+
# ๋ฒ์ญ ํ๋กฌํํธ
|
45 |
translation_prompt = f"""
|
46 |
+
Translate the following Korean text to English accurately:
|
|
|
47 |
|
48 |
+
{korean_text}
|
49 |
+
|
50 |
+
Provide only the translation, no explanations.
|
51 |
"""
|
52 |
|
53 |
+
# ๋ฒ์ญ ์์ฒญ
|
54 |
+
response = client.generate_content(translation_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# ์๋ต์์ ํ
์คํธ ์ถ์ถ
|
57 |
+
if hasattr(response, 'text'):
|
58 |
+
english_text = response.text.strip()
|
59 |
+
logger.info(f"๋ฒ์ญ ๊ฒฐ๊ณผ: {english_text}")
|
60 |
+
return english_text
|
61 |
+
else:
|
62 |
+
logger.warning("๋ฒ์ญ ์๋ต์ text ์์ฑ์ด ์์ต๋๋ค.")
|
63 |
+
return korean_text
|
64 |
except Exception as e:
|
65 |
logger.exception(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
66 |
+
return korean_text # ์ค๋ฅ ๋ฐ์ ์ ์๋ณธ ํ๊ตญ์ด ํ๋กฌํํธ ๋ฐํ
|
67 |
|
68 |
def save_binary_file(file_name, data):
|
69 |
with open(file_name, "wb") as f:
|
|
|
133 |
|
134 |
return prompt
|
135 |
|
136 |
+
def generate_with_images(prompt, images):
|
137 |
"""
|
138 |
๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ ์ฌ๋ฐ๋ฅธ API ํธ์ถ ๋ฐฉ์ ๊ตฌํ
|
139 |
์ฌ์๋ ๋ก์ง ์ถ๊ฐ
|
|
|
152 |
client = genai.Client(api_key=api_key)
|
153 |
|
154 |
# ํ๋กฌํํธ ๋ฒ์ญ
|
155 |
+
english_prompt = get_translation(prompt)
|
156 |
logger.info(f"์๋ณธ ํ๋กฌํํธ: {prompt}")
|
157 |
logger.info(f"๋ฒ์ญ๋ ํ๋กฌํํธ: {english_prompt}")
|
158 |
|
|
|
221 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
222 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
223 |
|
224 |
+
def process_images_with_prompt(image1, image2, image3, prompt):
|
225 |
"""
|
226 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
227 |
"""
|
|
|
249 |
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
250 |
|
251 |
# ์ด๋ฏธ์ง ์์ฑ API ํธ์ถ
|
252 |
+
return generate_with_images(prompt, images)
|
253 |
|
254 |
except Exception as e:
|
255 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
|
|
271 |
|
272 |
# Gradio ์ธํฐํ์ด์ค (์์ ๋ ๋ฒ์ )
|
273 |
def create_interface():
|
|
|
|
|
|
|
274 |
with gr.Blocks() as demo:
|
275 |
gr.HTML(
|
276 |
"""
|
|
|
334 |
def process_and_show_prompt(image1, image2, image3, prompt):
|
335 |
try:
|
336 |
# ์ด๋ฏธ์ง ์์ฑ ํจ์ ํธ์ถ
|
337 |
+
result_img, status = process_images_with_prompt(image1, image2, image3, prompt)
|
338 |
|
339 |
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ
|
340 |
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|