Download app.py from AMDF/minm: direct link, hf CLI and curl.
- Browser
- Download file 4.92 kB
-
https://huggingface.co/spaces/AMDF/minm/resolve/main/app.py
- Command line
-
hf download hf://spaces/AMDF/minm/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AMDF/minm/resolve/main/app.py
4.92 kB
| import gradio as gr | |
| import requests | |
| import json | |
| import os # os λΌμ΄λΈλ¬λ¦¬ μΆκ° | |
| # νκ²½ λ³μμμ API ν€λ₯Ό κ°μ Έμ΄ | |
| API_KEY = os.getenv("UPSTAGE_API_KEY") | |
| API_URL = "https://api.upstage.ai/v1/" | |
| # API ν€κ° μλ κ²½μ° μλ¬ λ°μ | |
| if API_KEY is None: | |
| raise ValueError("νκ²½ λ³μ 'UPSTAGE_API_KEY'κ° μ€μ λμ§ μμμ΅λλ€. API ν€λ₯Ό μ€μ ν΄μ£ΌμΈμ.") | |
| def chat_with_solar(message, history): | |
| """Solar Pro2μ λννλ ν¨μ""" | |
| messages = [ | |
| {"role": "system", "content": "λΉμ μ λμμ΄ λλ AI μ΄μμ€ν΄νΈμ λλ€. νκ΅μ΄λ‘ μΉκ·Όνκ³ μ ννκ² λ΅λ³ν΄μ£ΌμΈμ."} | |
| ] | |
| for human_msg, ai_msg in history: | |
| messages.append({"role": "user", "content": human_msg}) | |
| messages.append({"role": "assistant", "content": ai_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| data = { | |
| "model": "solar-pro-2", | |
| "messages": messages, | |
| "max_tokens": 1000, | |
| "temperature": 0.7 | |
| } | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| try: | |
| response = requests.post(API_URL, headers=headers, json=data) | |
| response.raise_for_status() | |
| result = response.json() | |
| ai_response = result['choices'][0]['message']['content'] | |
| return ai_response | |
| except requests.exceptions.RequestException as e: | |
| return f"API μμ² μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}" | |
| except (KeyError, IndexError) as e: | |
| return f"μλ΅ νμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}" | |
| except Exception as e: | |
| return f"μμμΉ λͺ»ν μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}" | |
| def create_chatbot(): | |
| """μ±λ΄ μΈν°νμ΄μ€ μμ±""" | |
| with gr.Blocks( | |
| title="Solar Pro2 μ±λ΄", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .container { max-width: 900px; margin: auto; } | |
| .header { text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; } | |
| """ | |
| ) as demo: | |
| gr.HTML(""" | |
| <div class="header"> | |
| <h1>π Solar Pro2 μ±λ΄</h1> | |
| <p>Upstageμ Solar Pro2 λͺ¨λΈκ³Ό λνν΄λ³΄μΈμ!</p> | |
| </div> | |
| """) | |
| chatbot = gr.Chatbot( | |
| height=500, | |
| bubble_full_width=False, | |
| avatar_images=("π€", "π€"), | |
| show_copy_button=True | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| placeholder="λ©μμ§λ₯Ό μ λ ₯νμΈμ...", | |
| container=False, | |
| scale=4, | |
| show_label=False | |
| ) | |
| submit_btn = gr.Button("μ μ‘", scale=1, variant="primary") | |
| clear_btn = gr.Button("λν μ΄κΈ°ν", scale=1) | |
| gr.Examples( | |
| examples=[ | |
| "μλ νμΈμ! μκΈ°μκ°λ₯Ό ν΄μ£ΌμΈμ.", | |
| "νμ΄μ¬μΌλ‘ κ°λ¨ν κ³μ°κΈ°λ₯Ό λ§λλ λ°©λ²μ μλ €μ£ΌμΈμ.", | |
| "μ€λ λ μ¨κ° μ’λ€μ. μ΄λ€ νλμ μΆμ²νμλμ?", | |
| "AIμ λν΄ κ°λ¨ν μ€λͺ ν΄μ£ΌμΈμ.", | |
| "λ§μλ νκ΅ μμ λ μνΌλ₯Ό νλ μΆμ²ν΄μ£ΌμΈμ." | |
| ], | |
| inputs=msg, | |
| label="μμ μ§λ¬Έλ€" | |
| ) | |
| with gr.Accordion("βΉοΈ μ¬μ© μ 보", open=False): | |
| gr.Markdown(""" | |
| ### Solar Pro2 μ±λ΄μ λν΄ | |
| - **λͺ¨λΈ**: Upstage Solar Pro2 | |
| - **κΈ°λ₯**: νκ΅μ΄ λν, μ§λ¬Έ λ΅λ³, μ½λ μμ± λ± | |
| - **νΉμ§**: λμ μ±λ₯μ νκ΅μ΄ μΈμ΄λͺ¨λΈ | |
| ### μ¬μ© λ°©λ² | |
| 1. μμ ν μ€νΈ λ°μ€μ μ§λ¬Έμ΄λ λνλ₯Ό μ λ ₯νμΈμ. | |
| 2. 'μ μ‘' λ²νΌμ ν΄λ¦νκ±°λ Enterλ₯Ό λλ₯΄μΈμ. | |
| 3. AIμ μλ΅μ νμΈνκ³ κ³μ λννμΈμ. | |
| ### μ£Όμμ¬ν | |
| - `UPSTAGE_API_KEY` νκ²½ λ³μκ° μ¬λ°λ₯΄κ² μ€μ λμ΄ μλμ§ νμΈν΄μ£ΌμΈμ. | |
| - μΈν°λ· μ°κ²°μ΄ νμν©λλ€. | |
| """) | |
| def respond(message, chat_history): | |
| if not message or not message.strip(): | |
| return "", chat_history | |
| bot_message = chat_with_solar(message, chat_history) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| # μ΄λ²€νΈ νΈλ€λ¬ μ°κ²° (μ€λ³΅ μ κ±°) | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| submit_btn.click(respond, [msg, chatbot], [msg, chatbot]) | |
| clear_btn.click(lambda: (None, []), outputs=[msg, chatbot]) | |
| return demo | |
| if __name__ == "__main__": | |
| demo = create_chatbot() | |
| demo.launch( | |
| share=True, | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| show_error=True, | |
| debug=True | |
| ) |