JUNGU commited on
Commit
3271c7b
Β·
verified Β·
1 Parent(s): 1b77547

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -53
app.py CHANGED
@@ -2,19 +2,46 @@ import gradio as gr
2
  import google.generativeai as genai
3
  import os
4
 
5
- # μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μ •μ˜
6
- SYSTEM_PROMPT = "λ°œν‘œ λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ£ΌλŠ” μ±—λ΄‡μž…λ‹ˆλ‹€. λ°œν‘œν•  λ‚΄μš©μ„ μž…λ ₯ν•˜λ©΄ 'μ™œλƒν•˜λ©΄ ~이기 λ•Œλ¬Έμž…λ‹ˆλ‹€'둜 λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ€λ‹ˆλ‹€"
7
-
8
- # API ν‚€ μ„€μ •
9
  api_key = os.environ.get("GEMINI_API_KEY")
10
  if api_key:
11
- genai.configure(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
12
 
13
  def chat(message, history):
 
 
 
14
  if not api_key:
15
- return history + [[message, "❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€."]]
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  try:
 
 
 
18
  model = genai.GenerativeModel(
19
  'gemini-1.5-flash',
20
  system_instruction=SYSTEM_PROMPT
@@ -28,59 +55,30 @@ def chat(message, history):
28
  if assistant:
29
  chat_history.append({"role": "model", "parts": [assistant]})
30
 
31
- # μ±„νŒ… μ„Έμ…˜ μ‹œμž‘ 및 응닡 생성
32
  chat_session = model.start_chat(history=chat_history)
33
- response = chat_session.send_message(message)
34
 
35
- # νžˆμŠ€ν† λ¦¬μ— μΆ”κ°€
36
- history.append([message, response.text])
37
- return history
38
 
39
  except Exception as e:
40
- history.append([message, f"❌ 였λ₯˜ λ°œμƒ: {str(e)}"])
41
- return history
 
 
 
42
 
43
- # Gradio Blocks μΈν„°νŽ˜μ΄μŠ€
44
- with gr.Blocks() as demo:
45
- gr.Markdown("""
46
- # πŸ€– λ…Όλ¦¬μ μœΌλ‘œ λ§ν•˜λŠ” λ°œν‘œλ„μš°λ―Έ Gemini 챗봇
47
- λ…Όλ¦¬μ μœΌλ‘œ λ°œν‘œν•˜λŠ” 방법을 λ„μ™€μ€λ‹ˆλ‹€.
48
- """)
49
-
50
- chatbot = gr.Chatbot()
51
- msg = gr.Textbox(
52
- placeholder="λ°œν‘œν•  λ‚΄μš©μ„ μž…λ ₯ν•˜μ„Έμš”...",
53
- label="λ©”μ‹œμ§€ μž…λ ₯"
54
- )
55
-
56
- with gr.Row():
57
- submit = gr.Button("전솑", variant="primary")
58
- clear = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”")
59
-
60
- # 예제 μ„Ήμ…˜
61
- gr.Markdown("### πŸ’‘ 예제 질문")
62
- with gr.Row():
63
- ex1 = gr.Button("ν•™κΈ‰νšŒμ˜λ•Œ λ§ν•˜λŠ” 방법", size="sm")
64
- ex2 = gr.Button("κ΅­μ–΄μ‹œκ°„μ— μ£Όμž₯ν•˜λŠ” 말 ν•˜λŠ” 방법", size="sm")
65
- ex3 = gr.Button("μ‚¬νšŒν˜„μƒμ— λŒ€ν•œ 의견", size="sm")
66
-
67
- # 이벀트 ν•Έλ“€λŸ¬
68
- def respond(message, chat_history):
69
- if message.strip():
70
- return chat(message, chat_history), ""
71
- return chat_history, ""
72
-
73
- # λ©”μ‹œμ§€ 전솑
74
- msg.submit(respond, [msg, chatbot], [chatbot, msg])
75
- submit.click(respond, [msg, chatbot], [chatbot, msg])
76
-
77
- # λŒ€ν™” μ΄ˆκΈ°ν™”
78
- clear.click(lambda: [], None, chatbot)
79
 
80
- # 예제 λ²„νŠΌ 클릭
81
- ex1.click(lambda h: chat("ν•™κΈ‰νšŒμ˜λ•Œ λ§ν•˜λŠ” 방법", h), [chatbot], [chatbot])
82
- ex2.click(lambda h: chat("κ΅­μ–΄μ‹œκ°„μ— μ£Όμž₯ν•˜λŠ” 말 ν•˜λŠ” 방법", h), [chatbot], [chatbot])
83
- ex3.click(lambda h: chat("μ‚¬νšŒν˜„μƒμ— λŒ€ν•œ 의견", h), [chatbot], [chatbot])
84
 
85
  if __name__ == "__main__":
86
  demo.launch()
 
2
  import google.generativeai as genai
3
  import os
4
 
5
+ # 디버깅: ν™˜κ²½λ³€μˆ˜ 확인
6
+ print("=== ν™˜κ²½λ³€μˆ˜ 디버깅 ===")
7
+ print(f"GEMINI_API_KEY 쑴재 μ—¬λΆ€: {'GEMINI_API_KEY' in os.environ}")
 
8
  api_key = os.environ.get("GEMINI_API_KEY")
9
  if api_key:
10
+ print(f"API ν‚€ 길이: {len(api_key)}")
11
+ print(f"API ν‚€ μ‹œμž‘: {api_key[:10]}...")
12
+ else:
13
+ print("API ν‚€κ°€ μ—†μŠ΅λ‹ˆλ‹€!")
14
+ print("μ‚¬μš© κ°€λŠ₯ν•œ ν™˜κ²½λ³€μˆ˜λ“€:")
15
+ for key in os.environ.keys():
16
+ if "KEY" in key or "SECRET" in key:
17
+ print(f" - {key}")
18
+
19
+ # μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ
20
+ SYSTEM_PROMPT = "λ°œν‘œ λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ£ΌλŠ” μ±—λ΄‡μž…λ‹ˆλ‹€. λ°œν‘œν•  λ‚΄μš©μ„ μž…λ ₯ν•˜λ©΄ 'μ™œλƒν•˜λ©΄ ~이기 λ•Œλ¬Έμž…λ‹ˆλ‹€'둜 λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ€λ‹ˆλ‹€"
21
 
22
  def chat(message, history):
23
+ # API ν‚€ λ‹€μ‹œ 확인
24
+ api_key = os.environ.get("GEMINI_API_KEY")
25
+
26
  if not api_key:
27
+ return f"""❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
28
+
29
+ **ν•΄κ²° 방법:**
30
+ 1. Hugging Face Space의 βš™οΈ Settings νƒ­μœΌλ‘œ 이동
31
+ 2. 'Repository secrets' μ„Ήμ…˜ μ°ΎκΈ°
32
+ 3. 'New secret' λ²„νŠΌ 클릭
33
+ 4. Name: `GEMINI_API_KEY` (μ •ν™•νžˆ μ΄λ ‡κ²Œ μž…λ ₯)
34
+ 5. Value: λ‹Ήμ‹ μ˜ Gemini API ν‚€ λΆ™μ—¬λ„£κΈ°
35
+ 6. 'Save' 클릭
36
+ 7. Spaceλ₯Ό μž¬μ‹œμž‘ (Settings β†’ Factory reboot)
37
+
38
+ ν˜„μž¬ μƒνƒœ: API ν‚€κ°€ {'있음' if api_key else 'μ—†μŒ'}
39
+ """
40
 
41
  try:
42
+ # API μ„€μ •
43
+ genai.configure(api_key=api_key)
44
+
45
  model = genai.GenerativeModel(
46
  'gemini-1.5-flash',
47
  system_instruction=SYSTEM_PROMPT
 
55
  if assistant:
56
  chat_history.append({"role": "model", "parts": [assistant]})
57
 
58
+ # μ±„νŒ… μ„Έμ…˜ μ‹œμž‘
59
  chat_session = model.start_chat(history=chat_history)
 
60
 
61
+ # 응닡 생성
62
+ response = chat_session.send_message(message)
63
+ return response.text
64
 
65
  except Exception as e:
66
+ error_msg = str(e)
67
+ if "API_KEY_INVALID" in error_msg:
68
+ return "❌ API ν‚€κ°€ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ˜¬λ°”λ₯Έ Gemini API 킀인지 ν™•μΈν•˜μ„Έμš”."
69
+ else:
70
+ return f"❌ 였λ₯˜ λ°œμƒ: {error_msg}"
71
 
72
+ # Gradio μΈν„°νŽ˜μ΄μŠ€
73
+ demo = gr.ChatInterface(
74
+ fn=chat,
75
+ title="πŸ€– λ…Όλ¦¬μ μœΌλ‘œ λ§ν•˜λŠ” λ°œν‘œλ„μš°λ―Έ Gemini 챗봇",
76
+ description=f"""λ…Όλ¦¬μ μœΌλ‘œ λ°œν‘œν•˜λŠ” 방법을 λ„μ™€μ€λ‹ˆλ‹€.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ **μƒνƒœ**: API ν‚€κ°€ {'βœ… 섀정됨' if api_key else '❌ μ„€μ •λ˜μ§€ μ•ŠμŒ'}
79
+ [API ν‚€ λ°›κΈ°](https://aistudio.google.com/app/apikey)
80
+ """
81
+ )
82
 
83
  if __name__ == "__main__":
84
  demo.launch()