Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,46 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# API ν€ μ€μ
|
| 9 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 10 |
if api_key:
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def chat(message, history):
|
|
|
|
|
|
|
|
|
|
| 14 |
if not api_key:
|
| 15 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 37 |
-
return
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Gradio
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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()
|