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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 환경변수에서 API 키 로드
|
| 7 |
+
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY", "")
|
| 8 |
+
|
| 9 |
+
def enhance_prompt(prompt: str, api_key: str) -> str:
|
| 10 |
+
"""
|
| 11 |
+
Fireworks AI LLM API를 사용하여 프롬프트를 증강합니다.
|
| 12 |
+
"""
|
| 13 |
+
if not prompt.strip():
|
| 14 |
+
return "❌ 프롬프트를 입력해주세요."
|
| 15 |
+
|
| 16 |
+
if not api_key.strip():
|
| 17 |
+
return "❌ Fireworks API 키를 입력해주세요."
|
| 18 |
+
|
| 19 |
+
system_message = """You are a professional prompt engineer specializing in AI image and video generation.
|
| 20 |
+
Your task is to enhance user prompts to create more detailed, vivid, and effective prompts for AI image/video generation models.
|
| 21 |
+
|
| 22 |
+
When enhancing prompts:
|
| 23 |
+
1. Add specific visual details (lighting, composition, style, mood)
|
| 24 |
+
2. Include artistic references or styles when appropriate
|
| 25 |
+
3. Specify camera angles, perspectives, and depth of field
|
| 26 |
+
4. Add color palette suggestions
|
| 27 |
+
5. Include quality modifiers (8K, cinematic, professional, etc.)
|
| 28 |
+
6. Maintain the original intent while enriching the description
|
| 29 |
+
7. Output the enhanced prompt in English for best AI model compatibility
|
| 30 |
+
|
| 31 |
+
Respond ONLY with the enhanced prompt, no explanations or additional text."""
|
| 32 |
+
|
| 33 |
+
url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 34 |
+
|
| 35 |
+
payload = {
|
| 36 |
+
"model": "accounts/fireworks/models/gpt-oss-120b",
|
| 37 |
+
"max_tokens": 4096,
|
| 38 |
+
"top_p": 1,
|
| 39 |
+
"top_k": 40,
|
| 40 |
+
"presence_penalty": 0,
|
| 41 |
+
"frequency_penalty": 0,
|
| 42 |
+
"temperature": 0.6,
|
| 43 |
+
"messages": [
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": system_message
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": f"다음 프롬프트를 이미지/영상 생성 AI에 최적화된 상세하고 풍부한 프롬프트로 증강해주세요:\n\n\"{prompt}\""
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
headers = {
|
| 56 |
+
"Accept": "application/json",
|
| 57 |
+
"Content-Type": "application/json",
|
| 58 |
+
"Authorization": f"Bearer {api_key}"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=60)
|
| 63 |
+
response.raise_for_status()
|
| 64 |
+
|
| 65 |
+
data = response.json()
|
| 66 |
+
enhanced = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 67 |
+
|
| 68 |
+
if enhanced:
|
| 69 |
+
return enhanced
|
| 70 |
+
else:
|
| 71 |
+
return "❌ 응답에서 증강된 프롬프트를 찾을 수 없습니다."
|
| 72 |
+
|
| 73 |
+
except requests.exceptions.Timeout:
|
| 74 |
+
return "❌ API 요청 시간 초과. 다시 시도해주세요."
|
| 75 |
+
except requests.exceptions.HTTPError as e:
|
| 76 |
+
return f"❌ API 오류: {e.response.status_code} - {e.response.text}"
|
| 77 |
+
except requests.exceptions.RequestException as e:
|
| 78 |
+
return f"❌ 네트워크 오류: {str(e)}"
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"❌ 오류 발생: {str(e)}"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Example prompts
|
| 84 |
+
examples = [
|
| 85 |
+
["한복을 입은 여성이 전통 한옥 마당에서 부채를 들고 있다"],
|
| 86 |
+
["사이버펑크 도시의 네온사인이 빛나는 밤거리"],
|
| 87 |
+
["바다 위 일몰과 작은 돛단배"],
|
| 88 |
+
["눈 덮인 산속 작은 오두막에서 연기가 피어오른다"],
|
| 89 |
+
["미래 도시의 하늘을 나는 자동차들"],
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
# Build the Gradio interface
|
| 93 |
+
with gr.Blocks(title="AI 프롬프트 증강기", theme=gr.themes.Soft(primary_hue="teal", secondary_hue="cyan")) as demo:
|
| 94 |
+
|
| 95 |
+
gr.HTML("""
|
| 96 |
+
<div style="text-align: center; padding: 20px 0;">
|
| 97 |
+
<div style="display: inline-flex; align-items: center; gap: 12px; margin-bottom: 10px;">
|
| 98 |
+
<div style="
|
| 99 |
+
width: 50px;
|
| 100 |
+
height: 50px;
|
| 101 |
+
border-radius: 12px;
|
| 102 |
+
background: linear-gradient(135deg, #4ecdc4 0%, #45b7d1 100%);
|
| 103 |
+
display: flex;
|
| 104 |
+
align-items: center;
|
| 105 |
+
justify-content: center;
|
| 106 |
+
font-size: 24px;
|
| 107 |
+
">✨</div>
|
| 108 |
+
<h1 style="
|
| 109 |
+
font-size: 2.5rem;
|
| 110 |
+
font-weight: 800;
|
| 111 |
+
background: linear-gradient(135deg, #4ecdc4 0%, #45b7d1 50%, #96e6a1 100%);
|
| 112 |
+
-webkit-background-clip: text;
|
| 113 |
+
-webkit-text-fill-color: transparent;
|
| 114 |
+
margin: 0;
|
| 115 |
+
">AI 프롬프트 증강기</h1>
|
| 116 |
+
</div>
|
| 117 |
+
<p style="color: #6b7280; font-size: 1.1rem; margin: 0;">
|
| 118 |
+
이미지 & 영상 생성을 위한 프롬프트를 AI가 자동으로 증강합니다
|
| 119 |
+
</p>
|
| 120 |
+
</div>
|
| 121 |
+
""")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
with gr.Column(scale=1):
|
| 125 |
+
api_key_input = gr.Textbox(
|
| 126 |
+
label="🔑 Fireworks API Key",
|
| 127 |
+
placeholder="fw_...",
|
| 128 |
+
type="password",
|
| 129 |
+
value=FIREWORKS_API_KEY,
|
| 130 |
+
info="Fireworks AI API 키를 입력하세요 (환경변수 FIREWORKS_API_KEY로도 설정 가능)"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column(scale=1):
|
| 135 |
+
prompt_input = gr.Textbox(
|
| 136 |
+
label="📝 원본 프롬프트",
|
| 137 |
+
placeholder="이미지나 영상으로 생성하고 싶은 장면을 설명해주세요...\n예: 한복을 입은 여성이 벚꽃 아래에서 걷고 있다",
|
| 138 |
+
lines=5,
|
| 139 |
+
max_lines=10
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
enhance_btn = gr.Button(
|
| 143 |
+
"✨ 프롬프트 증강하기",
|
| 144 |
+
variant="primary",
|
| 145 |
+
size="lg"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
gr.Examples(
|
| 149 |
+
examples=examples,
|
| 150 |
+
inputs=prompt_input,
|
| 151 |
+
label="💡 예시 프롬프트"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
with gr.Row():
|
| 155 |
+
with gr.Column(scale=1):
|
| 156 |
+
output_text = gr.Textbox(
|
| 157 |
+
label="🚀 증강된 프롬프트 (복사하여 사용하세요)",
|
| 158 |
+
placeholder="증강된 프롬프트가 여기에 표시됩니다...",
|
| 159 |
+
lines=8,
|
| 160 |
+
max_lines=15,
|
| 161 |
+
interactive=True
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
gr.HTML("""
|
| 165 |
+
<div style="text-align: center; padding: 20px 0; color: #6b7280; font-size: 0.9rem;">
|
| 166 |
+
<p>💡 <strong>팁:</strong> 증강된 프롬프트는 영어로 출력되어 대부분의 AI 이미지/영상 생성 모델과 호환됩니다.</p>
|
| 167 |
+
<p style="margin-top: 8px;">Powered by Fireworks AI (gpt-oss-120b) | Built with Gradio</p>
|
| 168 |
+
</div>
|
| 169 |
+
""")
|
| 170 |
+
|
| 171 |
+
# Event handlers
|
| 172 |
+
enhance_btn.click(
|
| 173 |
+
fn=enhance_prompt,
|
| 174 |
+
inputs=[prompt_input, api_key_input],
|
| 175 |
+
outputs=output_text,
|
| 176 |
+
show_progress=True
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Allow Enter key to submit (Shift+Enter for new line)
|
| 180 |
+
prompt_input.submit(
|
| 181 |
+
fn=enhance_prompt,
|
| 182 |
+
inputs=[prompt_input, api_key_input],
|
| 183 |
+
outputs=output_text,
|
| 184 |
+
show_progress=True
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
demo.launch()
|