Spaces:
Sleeping
Sleeping
| # generate_guide.py | |
| import openai | |
| import streamlit as st | |
| import os | |
| # π‘ OPENAI_API_KEY νκ²½ λ³μ νμΈ λ° μ€μ (μ΄κΈ°ν μ€λ³΅ λ°©μ§) | |
| def init_openai(): | |
| api_key = os.environ.get("OPENAI_API_KEY") | |
| if not api_key: | |
| raise RuntimeError( | |
| "β OPENAI_API_KEYκ° νκ²½λ³μλ‘ μ€μ λμ΄ μμ§ μμ΅λλ€. " | |
| "Hugging Face Secretsμ 'OPENAI_API_KEY'λ₯Ό λ±λ‘ν΄μ£ΌμΈμ." | |
| ) | |
| openai.api_key = api_key | |
| return openai | |
| # π 촬μ κ°μ΄λ μμ± ν¨μ | |
| def generate_photo_guide(user_caption: str, selected_image_descriptions: list) -> str: | |
| prompt = f""" | |
| μλλ μ¬μ©μκ° μ λ‘λν μ¬μ§ μ€λͺ μ λλ€: | |
| - {user_caption} | |
| μλλ μ¬μ©μκ° μ°Έκ³ νκ³ μΆλ€κ³ μ νν μ¬μ§ μ€λͺ λ€μ λλ€: | |
| {chr(10).join([f"- {desc}" for desc in selected_image_descriptions])} | |
| μ μ 보λ₯Ό μ’ ν©νμ¬, μ¬μ©μκ° μ¬μ§μ λ€μ μ°μ λ μ°Έκ³ ν μ μλλ‘ | |
| μλμ λ΄μ©μ μμ°μ΄λ‘ μμ±ν΄μ£ΌμΈμ: | |
| 1. μ΄λ€ ν¬μ¦λ₯Ό μ·¨νλ©΄ μ’μμ§ | |
| 2. μΉ΄λ©λΌ μμΉλ μ΄λμ λλ©΄ μ’μμ§ (λμ΄, κ°λ λ±) | |
| 3. μΈλ¬Ό λ°°μΉλ μ΄λ»κ² νλ©΄ μ’μμ§ (μ λ©΄/μΈ‘λ©΄, 거리 λ±) | |
| 4. μ 체 ꡬλ νμ΄ μλ€λ©΄ μλ €μ£ΌμΈμ | |
| """ | |
| try: | |
| init_openai() # β API ν€ μ€μ 보μ₯ | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.7, | |
| ) | |
| return response.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"β GPT νΈμΆμ μ€ν¨νμ΅λλ€: {str(e)}" | |