JUNGU commited on
Commit
cb245e5
ยท
verified ยท
1 Parent(s): 8c4c037

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -50
app.py CHANGED
@@ -1,64 +1,139 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
 
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
41
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
2
  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:
14
+ gr.Markdown(
15
+ """
16
+ # โ™Š๏ธ Gemini API ์ฑ—๋ด‡ (Secrets ์‚ฌ์šฉ)
17
+ Google Gemini API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฑ—๋ด‡์ž…๋‹ˆ๋‹ค.
18
+ Hugging Face Spaces์˜ 'Settings' ํƒญ์— ์žˆ๋Š” 'Repository secrets'์— `GEMINI_API_KEY`๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
19
+ [API ํ‚ค ๋ฐœ๊ธ‰๋ฐ›๊ธฐ](https://aistudio.google.com/app/apikey)
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
+ msg = gr.Textbox(
33
+ label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
34
+ placeholder="๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์„ธ์š”...",
35
+ container=False,
36
+ scale=7,
37
+ )
38
+ # ์ „์†ก ๋ฒ„ํŠผ
39
+ submit_button = gr.Button("์ „์†ก", variant="primary", scale=1)
40
 
41
+ with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ •", open=False):
42
+ # LLM์˜ ์—ญํ• ์„ ์ •์˜ํ•˜๋Š” ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€
43
+ system_message = gr.Textbox(
44
+ value="You are a helpful and friendly chatbot.", label="์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€"
45
+ )
46
+ # ๋ชจ๋ธ์˜ ์ฐฝ์˜์„ฑ์„ ์กฐ์ ˆํ•˜๋Š” ์Šฌ๋ผ์ด๋”
47
+ temperature = gr.Slider(
48
+ minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Temperature"
49
+ )
50
+ # ์ƒ์„ฑํ•  ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์Šฌ๋ผ์ด๋”
51
+ max_tokens = gr.Slider(
52
+ minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"
53
+ )
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:
63
+ # API ํ‚ค๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
64
+ genai.configure(api_key=GEMINI_API_KEY)
65
+ except Exception as e:
66
+ yield f"API ํ‚ค ์„ค์ •์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}"
67
+ return
68
+
69
+ # ์‚ฌ์šฉํ•  ๋ชจ๋ธ๊ณผ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
70
+ model = genai.GenerativeModel(
71
+ model_name='gemini-2.0-flash', # ์ตœ์‹  Flash ๋ชจ๋ธ ์‚ฌ์šฉ
72
+ system_instruction=system_prompt
73
+ )
74
 
75
+ # Gradio์˜ ๋Œ€ํ™” ๊ธฐ๋ก์„ Gemini API๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
76
+ gemini_history = []
77
+ for user_msg, model_msg in chat_history:
78
+ if user_msg:
79
+ gemini_history.append({"role": "user", "parts": [user_msg]})
80
+ if model_msg:
81
+ gemini_history.append({"role": "model", "parts": [model_msg]})
82
+
83
+ # ์ด์ „ ๋Œ€ํ™” ๊ธฐ๋ก์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฑ„ํŒ… ์„ธ์…˜์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
84
+ chat = model.start_chat(history=gemini_history)
85
 
86
+ # ๋ชจ๋ธ ์ƒ์„ฑ ๊ด€๋ จ ์„ค์ •์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
87
+ generation_config = genai.types.GenerationConfig(
88
+ temperature=temp,
89
+ max_output_tokens=int(max_output_tokens),
90
+ )
91
 
92
+ try:
93
+ # ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
94
+ response = chat.send_message(
95
+ message,
96
+ stream=True,
97
+ generation_config=generation_config
98
+ )
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+ # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ UI์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
101
+ full_response = ""
102
+ for chunk in response:
103
+ if hasattr(chunk, 'text'):
104
+ full_response += chunk.text
105
+ yield full_response
106
 
107
+ except Exception as e:
108
+ # API ํ˜ธ์ถœ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด UI์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
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],
129
+ [msg, chatbot]
130
+ )
131
+ submit_button.click(
132
+ on_submit,
133
+ [msg, chatbot, system_message, temperature, max_tokens],
134
+ [msg, chatbot]
135
+ )
136
+
137
+ # ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ง์ ‘ ์‹คํ–‰๋  ๋•Œ Gradio ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
138
  if __name__ == "__main__":
139
+ demo.launch(debug=True)