Spaces:
Sleeping
Sleeping
File size: 1,747 Bytes
eeaf866 629f478 eeaf866 00747b9 629f478 00747b9 eeaf866 629f478 eeaf866 629f478 eeaf866 00747b9 629f478 00747b9 629f478 | 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 | # generate_guide.py
import openai
import streamlit as st
import os
# π‘ OPENAI_API_KEY νκ²½ λ³μ νμΈ λ° μ€μ (μ΄κΈ°ν μ€λ³΅ λ°©μ§)
@st.cache_resource(show_spinner="OpenAI μ΄κΈ°ν μ€...")
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)}"
|