JUNGU commited on
Commit
30b8dce
Β·
verified Β·
1 Parent(s): 7ee4b9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
2
  import google.generativeai as genai
3
  import os
4
 
 
 
 
 
5
  # API ν‚€ μ„€μ •
6
  api_key = os.environ.get("GEMINI_API_KEY")
7
  if api_key:
@@ -9,11 +13,18 @@ if api_key:
9
 
10
  def chat(message, history):
11
  if not api_key:
12
- return "❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. HF Spaces Settingsμ—μ„œ GEMINI_API_KEYλ₯Ό μΆ”κ°€ν•˜μ„Έμš”."
13
 
 
 
14
  try:
15
- # Gemini λͺ¨λΈ μ΄ˆκΈ°ν™”
16
- model = genai.GenerativeModel('gemini-2.0-flash')
 
 
 
 
 
17
 
18
  # λŒ€ν™” 기둝 λ³€ν™˜
19
  chat_history = []
@@ -33,12 +44,12 @@ def chat(message, history):
33
  except Exception as e:
34
  return f"❌ 였λ₯˜ λ°œμƒ: {str(e)}"
35
 
36
- # Gradio μΈν„°νŽ˜μ΄μŠ€
37
  demo = gr.ChatInterface(
38
  fn=chat,
39
- title="πŸ€– Gemini 챗봇",
40
- description="Google Gemini APIλ₯Ό μ‚¬μš©ν•œ κ°„λ‹¨ν•œ μ±—λ΄‡μž…λ‹ˆλ‹€.",
41
- examples=["μ•ˆλ…•ν•˜μ„Έμš”!", "였늘 λ‚ μ”¨λŠ” μ–΄λ•Œ?", "νŒŒμ΄μ¬μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ€˜"],
42
  )
43
 
44
  if __name__ == "__main__":
 
2
  import google.generativeai as genai
3
  import os
4
 
5
+ # --- 1. μ±—λ΄‡μ˜ μ—­ν• (μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ)을 여기에 μ •μ˜ν•©λ‹ˆλ‹€. ---
6
+ # μ˜ˆμ‹œ: IT 및 μ½”λ”© μ§ˆλ¬Έμ— μ „λ¬Έμ μœΌλ‘œ λ‹΅λ³€ν•˜λŠ” AI μ–΄μ‹œμŠ€ν„΄νŠΈ
7
+ SYSTEM_PROMPT = "λ°œν‘œ λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ£ΌλŠ” μ±—λ΄‡μž…λ‹ˆλ‹€. λ°œν‘œν•  λ‚΄μš©μ„ μž…λ ₯ν•˜λ©΄ 'μ™œλƒν•˜λ©΄ ~이기 λ•Œλ¬Έμž…λ‹ˆλ‹€'둜 λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ€λ‹ˆλ‹€"
8
+
9
  # API ν‚€ μ„€μ •
10
  api_key = os.environ.get("GEMINI_API_KEY")
11
  if api_key:
 
13
 
14
  def chat(message, history):
15
  if not api_key:
16
+ return "❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. Hugging Face Spaces의 Settingsμ—μ„œ GEMINI_API_KEYλ₯Ό Secret으둜 μΆ”κ°€ν•˜μ„Έμš”."
17
 
18
+ # --- 2. ꡬ문 였λ₯˜ μˆ˜μ • ---
19
+ # try 블둝이 if λ¬Έκ³Ό λ™μΌν•œ λ ˆλ²¨μ— μžˆλ„λ‘ λ“€μ—¬μ“°κΈ°λ₯Ό μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€.
20
  try:
21
+ # --- 3. λͺ¨λΈ μ΄ˆκΈ°ν™” μ‹œ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ 적용 ---
22
+ # μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈλ₯Ό μ§€μ›ν•˜λŠ” 곡식 λͺ¨λΈλͺ…(gemini-1.5-flash)으둜 μˆ˜μ •ν•˜κ³ ,
23
+ # system_instruction νŒŒλΌλ―Έν„°μ— μœ„μ—μ„œ μ •μ˜ν•œ ν”„λ‘¬ν”„νŠΈλ₯Ό μ „λ‹¬ν•©λ‹ˆλ‹€.
24
+ model = genai.GenerativeModel(
25
+ 'gemini-2.0-flash',
26
+ system_instruction=SYSTEM_PROMPT
27
+ )
28
 
29
  # λŒ€ν™” 기둝 λ³€ν™˜
30
  chat_history = []
 
44
  except Exception as e:
45
  return f"❌ 였λ₯˜ λ°œμƒ: {str(e)}"
46
 
47
+ # Gradio μΈν„°νŽ˜μ΄μŠ€ (μš”μ²­λŒ€λ‘œ κ·ΈλŒ€λ‘œ μœ μ§€)
48
  demo = gr.ChatInterface(
49
  fn=chat,
50
+ title="πŸ€– λ…Όλ¦¬μ μœΌλ‘œ λ§ν•˜λŠ” λ°œν‘œλ„μš°λ―Έ Gemini 챗봇",
51
+ description="λ…Όλ¦¬μ μœΌλ‘œ λ°œν‘œν•˜λŠ” 방법을 λ„μ™€μ€λ‹ˆλ‹€.",
52
+ examples=["ν•™κΈ‰νšŒμ˜λ•Œ λ§ν•˜λŠ” 방법", "κ΅­μ–΄μ‹œκ°„μ— μ£Όμž₯ν•˜λŠ” 말 ν•˜λŠ” 방법", "μ‚¬νšŒν˜„μƒμ— λŒ€ν•œ 의견"],
53
  )
54
 
55
  if __name__ == "__main__":