JUNGU commited on
Commit
ad0c832
ยท
verified ยท
1 Parent(s): 226aa47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -18
app.py CHANGED
@@ -3,11 +3,6 @@ import gradio as gr
3
  import google.generativeai as genai
4
  import os
5
 
6
- # --- Hugging Face Spaces์˜ Secrets ๋˜๋Š” ๋กœ์ปฌ ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ---
7
- # ์ด ์•ฑ์„ Spaces์— ๋ฐฐํฌํ•  ๋•Œ๋Š” 'Settings' -> 'Repository secrets'์—
8
- # 'GEMINI_API_KEY'๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ‚ค๋ฅผ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
9
- GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
10
-
11
  # --- UI ๋ฐ ์ฑ—๋ด‡ ์„ค๋ช… ---
12
  # Gradio Blocks๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข€ ๋” ์œ ์—ฐํ•œ UI๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
13
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
@@ -20,16 +15,11 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
20
  """
21
  )
22
 
23
- # API ํ‚ค๊ฐ€ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
24
- if not GEMINI_API_KEY:
25
- gr.Warning("โš ๏ธ Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. Hugging Face Spaces์˜ 'Repository secrets'์— GEMINI_API_KEY๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.")
26
-
27
  # Gradio ์ฑ—๋ด‡ UI ์ปดํฌ๋„ŒํŠธ
28
  chatbot = gr.Chatbot(label="Gemini ์ฑ—๋ด‡", height=600)
29
 
30
  with gr.Row():
31
  # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ž…๋ ฅ๋ž€
32
- # container=False ์†์„ฑ์„ ์ œ๊ฑฐํ•˜์—ฌ Gradio ๋‚ด๋ถ€ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
33
  msg = gr.Textbox(
34
  label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
35
  placeholder="๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์„ธ์š”...",
@@ -54,9 +44,15 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
54
 
55
  # --- Gemini API ํ˜ธ์ถœ ํ•จ์ˆ˜ ---
56
  def respond(message, chat_history, system_prompt, temp, max_output_tokens):
 
 
 
 
57
  # ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜จ API ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ๋„์›๋‹ˆ๋‹ค.
58
  if not GEMINI_API_KEY:
59
- yield "Google API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•˜์„ธ์š”."
 
 
60
  return
61
 
62
  try:
@@ -109,20 +105,14 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
109
  yield f"์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}"
110
 
111
  # --- Gradio ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ---
112
- # ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜๊ณ  '์ „์†ก' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ์—”ํ„ฐ๋ฅผ ์ณค์„ ๋•Œ ์‹คํ–‰๋  ๋กœ์ง
113
  def on_submit(message, chat_history, system_prompt, temp, max_output_tokens):
114
- # ๋Œ€ํ™” ๊ธฐ๋ก์— ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
115
  chat_history.append((message, None))
116
- # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต์„ ์œ„ํ•ด ๋นˆ ๋ฌธ์ž์—ด๋กœ ์‘๋‹ต์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
117
  bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens)
118
 
119
- # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต์„ UI์— ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
120
  for partial_response in bot_response_stream:
121
  chat_history[-1] = (message, partial_response)
122
  yield "", chat_history
123
 
124
- # ๋ฉ”์‹œ์ง€ ์ „์†ก(๋ฒ„ํŠผ ํด๋ฆญ ๋˜๋Š” ์—”ํ„ฐ) ์‹œ on_submit ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
125
- # UI์—์„œ API ํ‚ค ์ž…๋ ฅ๋ž€์ด ์ œ๊ฑฐ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ž…๋ ฅ(inputs) ๋ฆฌ์ŠคํŠธ์—์„œ๋„ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค.
126
  msg.submit(
127
  on_submit,
128
  [msg, chatbot, system_message, temperature, max_tokens],
@@ -134,6 +124,5 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
134
  [msg, chatbot]
135
  )
136
 
137
- # ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ง์ ‘ ์‹คํ–‰๋  ๋•Œ Gradio ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
138
  if __name__ == "__main__":
139
  demo.launch(debug=True)
 
3
  import google.generativeai as genai
4
  import os
5
 
 
 
 
 
 
6
  # --- UI ๋ฐ ์ฑ—๋ด‡ ์„ค๋ช… ---
7
  # Gradio Blocks๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข€ ๋” ์œ ์—ฐํ•œ UI๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
8
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
 
15
  """
16
  )
17
 
 
 
 
 
18
  # Gradio ์ฑ—๋ด‡ UI ์ปดํฌ๋„ŒํŠธ
19
  chatbot = gr.Chatbot(label="Gemini ์ฑ—๋ด‡", height=600)
20
 
21
  with gr.Row():
22
  # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ž…๋ ฅ๋ž€
 
23
  msg = gr.Textbox(
24
  label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
25
  placeholder="๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์„ธ์š”...",
 
44
 
45
  # --- Gemini API ํ˜ธ์ถœ ํ•จ์ˆ˜ ---
46
  def respond(message, chat_history, system_prompt, temp, max_output_tokens):
47
+ # ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ํ‚ค๋ฅผ ์ง์ ‘ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
48
+ # ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ฑ ์‹œ์ž‘ ์‹œ์ ์— ํ‚ค๋ฅผ ๋ชป ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
49
+ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
50
+
51
  # ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜จ API ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ๋„์›๋‹ˆ๋‹ค.
52
  if not GEMINI_API_KEY:
53
+ # UI์— ์ง์ ‘ ๊ฒฝ๊ณ ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด gr.Warning์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,
54
+ # ์—ฌ๊ธฐ์„œ๋Š” ์ฑ„ํŒ… ์‘๋‹ต์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
55
+ yield "โš ๏ธ **์˜ค๋ฅ˜**: `GEMINI_API_KEY`๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.\n\nHugging Face Spaces์˜ **Settings > Repository secrets**์— API ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
56
  return
57
 
58
  try:
 
105
  yield f"์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}"
106
 
107
  # --- Gradio ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ---
 
108
  def on_submit(message, chat_history, system_prompt, temp, max_output_tokens):
 
109
  chat_history.append((message, None))
 
110
  bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens)
111
 
 
112
  for partial_response in bot_response_stream:
113
  chat_history[-1] = (message, partial_response)
114
  yield "", chat_history
115
 
 
 
116
  msg.submit(
117
  on_submit,
118
  [msg, chatbot, system_message, temperature, max_tokens],
 
124
  [msg, chatbot]
125
  )
126
 
 
127
  if __name__ == "__main__":
128
  demo.launch(debug=True)