ssboost commited on
Commit
37cd1bd
Β·
verified Β·
1 Parent(s): 5cb4fa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -48
app.py CHANGED
@@ -1,42 +1,4 @@
1
- try:
2
- # 2.0 λ²„μ „μš© μ•ˆμ •μ μΈ API 호좜
3
- response = self.model.generate_content(
4
- contents=f"{system_prompt}\n\nμ‚¬μš©μž 질문: {message}",
5
- generation_config=genai.GenerationConfig(
6
- temperature=0.1,
7
- max_output_tokens=1000,
8
- ),
9
- stream=True
10
- )
11
-
12
- partial_message = ""
13
- for chunk in response:
14
- try:
15
- if chunk.text:
16
- partial_message += chunk.text
17
- yield partial_message
18
- except Exception as chunk_error:
19
- # κ°œλ³„ 청크 μ—λŸ¬λŠ” λ¬΄μ‹œν•˜κ³  계속 μ§„ν–‰
20
- continue
21
-
22
- except Exception as e:
23
- # μ—λŸ¬ λ‚΄μš©μ„ 더 μžμ„Ένžˆ 보기 μœ„ν•΄
24
- print(f"μ—λŸ¬ λ°œμƒ: {str(e)}")
25
-
26
- # 슀트리밍이 μ•ˆλ˜λ©΄ 일반 λ°©μ‹μœΌλ‘œ μ‹œλ„
27
- try:
28
- response = self.model.generate_content(
29
- contents=f"{system_prompt}\n\nμ‚¬μš©μž 질문: {message}",
30
- generation_config=genai.GenerationConfig(
31
- temperature=0.1,
32
- max_output_tokens=1000,
33
- ),
34
- stream=False
35
- )
36
- yield response.text
37
- except Exception as fallback_error:
38
- print(f"Fallback μ—λŸ¬: {str(fallback_error)}")
39
- yield f"μ£„μ†‘ν•©λ‹ˆλ‹€. μ‹œμŠ€ν…œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 고객센터(010-8082-0047)둜 μ—°λ½ν•΄μ£Όμ„Έμš”."import gradio as gr
40
  import google.generativeai as genai
41
  import os
42
 
@@ -49,7 +11,6 @@ class SimpleBanchanChatbot:
49
 
50
  # μ œλ―Έλ‚˜μ΄ ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
51
  genai.configure(api_key=api_key)
52
- # 2.0 버전 μ‚¬μš©
53
  self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
54
 
55
  # μ—‘μ…€μ—μ„œ μΆ”μΆœν•œ λͺ¨λ“  FAQ 데이터
@@ -156,23 +117,38 @@ class SimpleBanchanChatbot:
156
 
157
  try:
158
  response = self.model.generate_content(
159
- message,
160
- generation_config=genai.types.GenerationConfig(
161
  temperature=0.1,
162
  max_output_tokens=1000,
163
  ),
164
- system_instruction=system_prompt,
165
  stream=True
166
  )
167
 
168
  partial_message = ""
169
  for chunk in response:
170
- if chunk.text:
171
- partial_message += chunk.text
172
- yield partial_message
 
 
 
173
 
174
  except Exception as e:
175
- yield f"μ£„μ†‘ν•©λ‹ˆλ‹€. μ‹œμŠ€ν…œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 고객센터(010-8082-0047)둜 μ—°λ½ν•΄μ£Όμ„Έμš”."
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  # 챗봇 μΈμŠ€ν„΄μŠ€ 생성
178
  try:
@@ -203,7 +179,6 @@ try:
203
  )
204
 
205
  except Exception as e:
206
- # API ν‚€κ°€ 없을 λ•Œ μ—λŸ¬ λ©”μ‹œμ§€ ν‘œμ‹œ
207
  def error_message(message, history):
208
  yield "❌ GEMINI_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. κ΄€λ¦¬μžμ—κ²Œ λ¬Έμ˜ν•˜μ„Έμš”."
209
 
 
1
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import google.generativeai as genai
3
  import os
4
 
 
11
 
12
  # μ œλ―Έλ‚˜μ΄ ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
13
  genai.configure(api_key=api_key)
 
14
  self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
15
 
16
  # μ—‘μ…€μ—μ„œ μΆ”μΆœν•œ λͺ¨λ“  FAQ 데이터
 
117
 
118
  try:
119
  response = self.model.generate_content(
120
+ contents=f"{system_prompt}\n\nμ‚¬μš©μž 질문: {message}",
121
+ generation_config=genai.GenerationConfig(
122
  temperature=0.1,
123
  max_output_tokens=1000,
124
  ),
 
125
  stream=True
126
  )
127
 
128
  partial_message = ""
129
  for chunk in response:
130
+ try:
131
+ if chunk.text:
132
+ partial_message += chunk.text
133
+ yield partial_message
134
+ except:
135
+ continue
136
 
137
  except Exception as e:
138
+ print(f"μ—λŸ¬ λ°œμƒ: {str(e)}")
139
+ try:
140
+ response = self.model.generate_content(
141
+ contents=f"{system_prompt}\n\nμ‚¬μš©μž 질문: {message}",
142
+ generation_config=genai.GenerationConfig(
143
+ temperature=0.1,
144
+ max_output_tokens=1000,
145
+ ),
146
+ stream=False
147
+ )
148
+ yield response.text
149
+ except Exception as fallback_error:
150
+ print(f"Fallback μ—λŸ¬: {str(fallback_error)}")
151
+ yield f"μ£„μ†‘ν•©λ‹ˆλ‹€. μ‹œμŠ€ν…œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 고객센터(010-8082-0047)둜 μ—°λ½ν•΄μ£Όμ„Έμš”."
152
 
153
  # 챗봇 μΈμŠ€ν„΄μŠ€ 생성
154
  try:
 
179
  )
180
 
181
  except Exception as e:
 
182
  def error_message(message, history):
183
  yield "❌ GEMINI_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. κ΄€λ¦¬μžμ—κ²Œ λ¬Έμ˜ν•˜μ„Έμš”."
184