Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import os
|
4 |
|
5 |
# λλ²κΉ
: νκ²½λ³μ νμΈ
|
6 |
print("=== νκ²½λ³μ λλ²κΉ
===")
|
7 |
-
print(f"
|
8 |
-
api_key = os.environ.get("
|
9 |
if api_key:
|
10 |
print(f"API ν€ κΈΈμ΄: {len(api_key)}")
|
11 |
print(f"API ν€ μμ: {api_key[:10]}...")
|
@@ -21,63 +21,78 @@ SYSTEM_PROMPT = "λ°ν λλ³Έμ λ§λ€μ΄μ£Όλ μ±λ΄μ
λλ€. λ°νν λ΄
|
|
21 |
|
22 |
def chat(message, history):
|
23 |
# API ν€ λ€μ νμΈ
|
24 |
-
api_key = os.environ.get("
|
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: `
|
34 |
-
5. Value: λΉμ μ
|
35 |
6. 'Save' ν΄λ¦
|
36 |
7. Spaceλ₯Ό μ¬μμ (Settings β Factory reboot)
|
37 |
-
|
38 |
νμ¬ μν: API ν€κ° {'μμ' if api_key else 'μμ'}
|
39 |
"""
|
40 |
|
41 |
try:
|
42 |
-
# API μ€μ
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
'gemini-1.5-flash',
|
47 |
-
system_instruction=SYSTEM_PROMPT
|
48 |
)
|
49 |
|
50 |
-
# λν κΈ°λ‘ λ³ν
|
51 |
-
|
|
|
|
|
52 |
for human, assistant in history:
|
53 |
if human:
|
54 |
-
|
55 |
if assistant:
|
56 |
-
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
|
61 |
-
#
|
62 |
-
response =
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
except Exception as e:
|
66 |
error_msg = str(e)
|
67 |
-
if "
|
68 |
-
return "β API ν€κ° μ ν¨νμ§ μμ΅λλ€. μ¬λ°λ₯Έ
|
|
|
|
|
69 |
else:
|
70 |
return f"β μ€λ₯ λ°μ: {error_msg}"
|
71 |
|
72 |
# Gradio μΈν°νμ΄μ€
|
73 |
demo = gr.ChatInterface(
|
74 |
fn=chat,
|
75 |
-
title="π€ λ
Όλ¦¬μ μΌλ‘ λ§νλ λ°νλμ°λ―Έ
|
76 |
description=f"""λ
Όλ¦¬μ μΌλ‘ λ°ννλ λ°©λ²μ λμμ€λλ€.
|
77 |
|
78 |
**μν**: API ν€κ° {'β
μ€μ λ¨' if api_key else 'β μ€μ λμ§ μμ'}
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
import os
|
4 |
|
5 |
# λλ²κΉ
: νκ²½λ³μ νμΈ
|
6 |
print("=== νκ²½λ³μ λλ²κΉ
===")
|
7 |
+
print(f"UPSTAGE_API_KEY μ‘΄μ¬ μ¬λΆ: {'UPSTAGE_API_KEY' in os.environ}")
|
8 |
+
api_key = os.environ.get("UPSTAGE_API_KEY")
|
9 |
if api_key:
|
10 |
print(f"API ν€ κΈΈμ΄: {len(api_key)}")
|
11 |
print(f"API ν€ μμ: {api_key[:10]}...")
|
|
|
21 |
|
22 |
def chat(message, history):
|
23 |
# API ν€ λ€μ νμΈ
|
24 |
+
api_key = os.environ.get("UPSTAGE_API_KEY")
|
25 |
|
26 |
if not api_key:
|
27 |
return f"""β API ν€κ° μ€μ λμ§ μμμ΅λλ€.
|
|
|
28 |
**ν΄κ²° λ°©λ²:**
|
29 |
1. Hugging Face Spaceμ βοΈ Settings νμΌλ‘ μ΄λ
|
30 |
2. 'Repository secrets' μΉμ
μ°ΎκΈ°
|
31 |
3. 'New secret' λ²νΌ ν΄λ¦
|
32 |
+
4. Name: `UPSTAGE_API_KEY` (μ νν μ΄λ κ² μ
λ ₯)
|
33 |
+
5. Value: λΉμ μ Upstage API ν€ λΆμ¬λ£κΈ° (up_μΌλ‘ μμ)
|
34 |
6. 'Save' ν΄λ¦
|
35 |
7. Spaceλ₯Ό μ¬μμ (Settings β Factory reboot)
|
|
|
36 |
νμ¬ μν: API ν€κ° {'μμ' if api_key else 'μμ'}
|
37 |
"""
|
38 |
|
39 |
try:
|
40 |
+
# Solar API ν΄λΌμ΄μΈνΈ μ€μ
|
41 |
+
client = OpenAI(
|
42 |
+
api_key=api_key,
|
43 |
+
base_url="https://api.upstage.ai/v1"
|
|
|
|
|
44 |
)
|
45 |
|
46 |
+
# λν κΈ°λ‘ λ³ν - Solar APIλ OpenAI νμ μ¬μ©
|
47 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
48 |
+
|
49 |
+
# μ΄μ λν κΈ°λ‘ μΆκ°
|
50 |
for human, assistant in history:
|
51 |
if human:
|
52 |
+
messages.append({"role": "user", "content": human})
|
53 |
if assistant:
|
54 |
+
messages.append({"role": "assistant", "content": assistant})
|
55 |
|
56 |
+
# νμ¬ λ©μμ§ μΆκ°
|
57 |
+
messages.append({"role": "user", "content": message})
|
58 |
|
59 |
+
# Solar API νΈμΆ
|
60 |
+
response = client.chat.completions.create(
|
61 |
+
model="solar-pro", # solar-pro λλ solar-mini μ¬μ© κ°λ₯
|
62 |
+
messages=messages,
|
63 |
+
stream=False # stream=Trueλ‘ νλ©΄ μ€μκ° μ€νΈλ¦¬λ° κ°λ₯
|
64 |
+
)
|
65 |
+
|
66 |
+
return response.choices[0].message.content
|
67 |
|
68 |
except Exception as e:
|
69 |
error_msg = str(e)
|
70 |
+
if "401" in error_msg or "Unauthorized" in error_msg:
|
71 |
+
return "β API ν€κ° μ ν¨νμ§ μμ΅λλ€. μ¬λ°λ₯Έ Upstage API ν€μΈμ§ νμΈνμΈμ. (up_λ‘ μμν΄μΌ ν¨)"
|
72 |
+
elif "quota" in error_msg.lower() or "limit" in error_msg.lower():
|
73 |
+
return "β API μ¬μ©λ νλλ₯Ό μ΄κ³Όνμ΅λλ€. λμ€μ λ€μ μλνκ±°λ μκΈμ λ₯Ό νμΈνμΈμ."
|
74 |
else:
|
75 |
return f"β μ€λ₯ λ°μ: {error_msg}"
|
76 |
|
77 |
# Gradio μΈν°νμ΄μ€
|
78 |
demo = gr.ChatInterface(
|
79 |
fn=chat,
|
80 |
+
title="π€ λ
Όλ¦¬μ μΌλ‘ λ§νλ λ°νλμ°λ―Έ Solar μ±λ΄",
|
81 |
description=f"""λ
Όλ¦¬μ μΌλ‘ λ°ννλ λ°©λ²μ λμμ€λλ€.
|
82 |
|
83 |
**μν**: API ν€κ° {'β
μ€μ λ¨' if api_key else 'β μ€μ λμ§ μμ'}
|
84 |
+
|
85 |
+
**μ¬μ© λͺ¨λΈ**: Solar Pro (Upstage AI)
|
86 |
+
[API ν€ λ°κΈ°](https://console.upstage.ai/)
|
87 |
+
|
88 |
+
π‘ **AI Initiative νλ‘κ·Έλ¨ μ°Έμ¬μλΌλ©΄ solar-proλ₯Ό 무λ£λ‘ μ¬μ©ν μ μμ΅λλ€!**
|
89 |
+
[AI Initiative μ μ²νκΈ°](https://www.upstage.ai/events/ai-initiative-2025-ko)
|
90 |
+
""",
|
91 |
+
examples=[
|
92 |
+
"μΈκ³΅μ§λ₯μ λ―Έλμ λν΄ λ°ν λλ³Έμ λ§λ€μ΄μ£ΌμΈμ",
|
93 |
+
"νκ²½ 보νΈμ μ€μμ±μ λν λ°νλ¬Έμ μμ±ν΄μ£ΌμΈμ",
|
94 |
+
"νμν¬μ μ€μμ±μ κ°μ‘°νλ λ°ν μλ£λ₯Ό λ§λ€μ΄μ£ΌμΈμ"
|
95 |
+
]
|
96 |
)
|
97 |
|
98 |
if __name__ == "__main__":
|