Spaces:
Running
Running
File size: 3,565 Bytes
226a535 35e0b37 7309bb0 3271c7b 35e0b37 732091b 3271c7b d034f8c 732091b 3271c7b 35e0b37 3271c7b 732091b 3271c7b 35e0b37 3271c7b cb245e5 732091b 35e0b37 30b8dce 732091b 35e0b37 732091b 35e0b37 732091b 35e0b37 732091b 35e0b37 1b77547 35e0b37 25c5250 35e0b37 732091b 3271c7b 35e0b37 3271c7b 732091b 3271c7b 35e0b37 3271c7b 1b77547 3271c7b 35e0b37 1a10419 3271c7b cb245e5 226a535 732091b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import gradio as gr
from openai import OpenAI
import os
# λλ²κΉ
: νκ²½λ³μ νμΈ
print("=== νκ²½λ³μ λλ²κΉ
===")
print(f"UPSTAGE_API_KEY μ‘΄μ¬ μ¬λΆ: {'UPSTAGE_API_KEY' in os.environ}")
api_key = os.environ.get("UPSTAGE_API_KEY")
if api_key:
print(f"API ν€ κΈΈμ΄: {len(api_key)}")
print(f"API ν€ μμ: {api_key[:10]}...")
else:
print("API ν€κ° μμ΅λλ€!")
print("μ¬μ© κ°λ₯ν νκ²½λ³μλ€:")
for key in os.environ.keys():
if "KEY" in key or "SECRET" in key:
print(f" - {key}")
# μμ€ν
ν둬ννΈ
SYSTEM_PROMPT = "λ°ν λλ³Έμ λ§λ€μ΄μ£Όλ μ±λ΄μ
λλ€. λ°νν λ΄μ©μ μ
λ ₯νλ©΄ 'μλνλ©΄ ~μ΄κΈ° λλ¬Έμ
λλ€'λ‘ λλ³Έμ λ§λ€μ΄μ€λλ€"
def chat(message, history):
# API ν€ λ€μ νμΈ
api_key = os.environ.get("UPSTAGE_API_KEY")
if not api_key:
return f"""β API ν€κ° μ€μ λμ§ μμμ΅λλ€.
**ν΄κ²° λ°©λ²:**
1. Hugging Face Spaceμ βοΈ Settings νμΌλ‘ μ΄λ
2. 'Repository secrets' μΉμ
μ°ΎκΈ°
3. 'New secret' λ²νΌ ν΄λ¦
4. Name: `UPSTAGE_API_KEY` (μ νν μ΄λ κ² μ
λ ₯)
5. Value: λΉμ μ Upstage API ν€ λΆμ¬λ£κΈ° (up_μΌλ‘ μμ)
6. 'Save' ν΄λ¦
7. Spaceλ₯Ό μ¬μμ (Settings β Factory reboot)
νμ¬ μν: API ν€κ° {'μμ' if api_key else 'μμ'}
"""
try:
# Solar API ν΄λΌμ΄μΈνΈ μ€μ
client = OpenAI(
api_key=api_key,
base_url="https://api.upstage.ai/v1"
)
# λν κΈ°λ‘ λ³ν - Solar APIλ OpenAI νμ μ¬μ©
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
# μ΄μ λν κΈ°λ‘ μΆκ°
for human, assistant in history:
if human:
messages.append({"role": "user", "content": human})
if assistant:
messages.append({"role": "assistant", "content": assistant})
# νμ¬ λ©μμ§ μΆκ°
messages.append({"role": "user", "content": message})
# Solar API νΈμΆ
response = client.chat.completions.create(
model="solar-pro2-preview", # solar-pro λλ solar-mini μ¬μ© κ°λ₯
messages=messages,
stream=False # stream=Trueλ‘ νλ©΄ μ€μκ° μ€νΈλ¦¬λ° κ°λ₯
)
return response.choices[0].message.content
except Exception as e:
error_msg = str(e)
if "401" in error_msg or "Unauthorized" in error_msg:
return "β API ν€κ° μ ν¨νμ§ μμ΅λλ€. μ¬λ°λ₯Έ Upstage API ν€μΈμ§ νμΈνμΈμ. (up_λ‘ μμν΄μΌ ν¨)"
elif "quota" in error_msg.lower() or "limit" in error_msg.lower():
return "β API μ¬μ©λ νλλ₯Ό μ΄κ³Όνμ΅λλ€. λμ€μ λ€μ μλνκ±°λ μκΈμ λ₯Ό νμΈνμΈμ."
else:
return f"β μ€λ₯ λ°μ: {error_msg}"
# Gradio μΈν°νμ΄μ€
demo = gr.ChatInterface(
fn=chat,
title="π€ λ
Όλ¦¬μ μΌλ‘ λ§νλ λ°νλμ°λ―Έ Solar μ±λ΄",
description=f"""λ
Όλ¦¬μ μΌλ‘ λ°ννλ λ°©λ²μ λμμ€λλ€.
**μν**: API ν€κ° {'β
μ€μ λ¨' if api_key else 'β μ€μ λμ§ μμ'}
**μ¬μ© λͺ¨λΈ**: Solar Pro (Upstage AI)
[API ν€ λ°κΈ°](https://console.upstage.ai/)
π‘ **AI Initiative νλ‘κ·Έλ¨ μ°Έμ¬μλΌλ©΄ solar-proλ₯Ό 무λ£λ‘ μ¬μ©ν μ μμ΅λλ€!**
[AI Initiative μ μ²νκΈ°](https://www.upstage.ai/events/ai-initiative-2025-ko)
"""
)
if __name__ == "__main__":
demo.launch() |