Spaces:
Sleeping
Sleeping
| 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() |