Update app.py
Browse files1234567891011121314157 unmodified lines2324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990913 unmodified lines95969798991001011021031041053 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771783 unmodified lines1821831841851861871881891901911926 unmodified lines199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561import gradio as grimport numpy as npimport cv2from fastapi import FastAPI, Request, Responsefrom src.body import Bodyimport json as jsimport requestsimport osfrom typing import Dict, List, Tuple# Initialize body estimation modelbody_estimation = Body('model/body_pose_model.pth')# Fireworks AI configurationFIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY", "YOUR_API_KEY_HERE")7 unmodified lines"LEye": 15, "REar": 16, "LEar": 17}# Pose templates for common positionsPOSE_TEMPLATES = {"standing": {"keypoints": {"Neck": [256, 120],"RShoulder": [220, 140], "RElbow": [200, 200], "RWrist": [190, 260],"LShoulder": [292, 140], "LElbow": [312, 200], "LWrist": [322, 260],"RHip": [230, 280], "RKnee": [225, 380], "RAnkle": [220, 480],"LHip": [282, 280], "LKnee": [287, 380], "LAnkle": [292, 480]}},"sitting": {"keypoints": {"Neck": [256, 180],"RShoulder": [220, 200], "RElbow": [200, 260], "RWrist": [190, 320],"LShoulder": [292, 200], "LElbow": [312, 260], "LWrist": [322, 320],"RHip": [230, 340], "RKnee": [225, 400], "RAnkle": [280, 420],"LHip": [282, 340], "LKnee": [287, 400], "LAnkle": [232, 420]}},"running": {"keypoints": {"Neck": [256, 120],"RShoulder": [220, 140], "RElbow": [180, 180], "RWrist": [150, 220],"LShoulder": [292, 140], "LElbow": [332, 180], "LWrist": [362, 140],"RHip": [230, 280], "RKnee": [260, 380], "RAnkle": [290, 470],"LHip": [282, 280], "LKnee": [252, 360], "LAnkle": [222, 440]}},"yoga": {"keypoints": {"Neck": [256, 140],"RShoulder": [200, 160], "RElbow": [150, 120], "RWrist": [100, 100],"LShoulder": [312, 160], "LElbow": [362, 120], "LWrist": [412, 100],"RHip": [240, 300], "RKnee": [220, 400], "RAnkle": [200, 480],"LHip": [272, 300], "LKnee": [292, 400], "LAnkle": [312, 480]}}}def pil2cv(image):'''PIL型 -> OpenCV��'''new_image = np.array(image, dtype=np.uint8)if new_image.ndim == 2: # モノクロpasselif new_image.shape[2] == 3: # カラーnew_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)elif new_image.shape[2] == 4: # 透過new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)return new_imagedef generate_pose_from_llm_sync(prompt: str) -> Dict:"""LLM을 사용하여 텍스트 프롬프트로부터 포즈 데이터를 생성"""system_prompt = """You are an expert in human pose generation. Generate precise OpenPose keypoint coordinates.Canvas: 512x512 pixelsReturn 18 keypoints with format: [[x, y, confidence], ...]Keypoints: 0:Nose, 1:Neck, 2:RShoulder, 3:RElbow, 4:RWrist, 5:LShoulder, 6:LElbow, 7:LWrist,8:RHip, 9:RKnee, 10:RAnkle, 11:LHip, 12:LKnee, 13:LAnkle, 14:REye, 15:LEye, 16:REar, 17:LEarReturn JSON: {"candidate": [[x,y,1.0],...], "subset": [[connected_indices, score, count]]}"""headers = {"Accept": "application/json",3 unmodified linespayload = {"model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507","max_tokens": 2048,"temperature": 0.3,"messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": f"Generate OpenPose keypoints for: {prompt}"}]}3 unmodified linesdata = response.json()content = data['choices'][0]['message']['content']import rejson_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:pose_data = js.loads(json_match.group())return pose_dataexcept Exception as e:print(f"LLM Error: {e}")return generate_template_pose(prompt)def generate_template_pose(prompt: str) -> Dict:"""템플릿 기반 포즈 생성"""prompt_lower = prompt.lower()# Select template based on keywordsif any(word in prompt_lower for word in ["sit", "sitting", "seated", "chair"]):template = POSE_TEMPLATES["sitting"]elif any(word in prompt_lower for word in ["run", "running", "jog", "sprint"]):template = POSE_TEMPLATES["running"]elif any(word in prompt_lower for word in ["yoga", "warrior", "meditation", "stretch"]):template = POSE_TEMPLATES["yoga"]else:template = POSE_TEMPLATES["standing"]# Build candidate arraycandidate = []for i in range(18):if i == 0: # Nosecandidate.append([256, 100, 1.0])elif i == 14: # REyecandidate.append([246, 90, 1.0])elif i == 15: # LEyecandidate.append([266, 90, 1.0])elif i == 16: # REarcandidate.append([236, 95, 1.0])elif i == 17: # LEarcandidate.append([276, 95, 1.0])else:part_name = Nonefor name, idx in BODY_PARTS.items():if idx == i:part_name = namebreakif part_name and part_name in template["keypoints"]:x, y = template["keypoints"][part_name]candidate.append([x, y, 1.0])else:candidate.append([256, 256, 0.0])# Create subsetvalid_indices = [i for i in range(18) if candidate[i][2] > 0]subset = [valid_indices + [float(len(valid_indices)), len(valid_indices)]]return {"candidate": candidate, "subset": subset}def refine_pose_with_llm(current_pose: Dict, refinement_prompt: str) -> Dict:"""기존 포즈를 LLM으로 세밀하게 조정"""system_prompt = """Modify the given pose keypoints based on instructions.Maintain anatomical correctness. Return same JSON format."""headers = {"Accept": "application/json",3 unmodified linespayload = {"model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507","max_tokens": 2048,"temperature": 0.2,"messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": f"Current: {js.dumps(current_pose)}\nAdjust: {refinement_prompt}"}]}6 unmodified linesimport rejson_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:return js.loads(json_match.group())except Exception as e:print(f"Refinement error: {e}")return current_pose# Load JavaScript filetry:with open("static/poseEditor.js", "r") as f:file_contents = f.read()except:file_contents = "console.log('PoseEditor.js not found');"app = FastAPI()@app.middleware("http")async def some_fastapi_middleware(request: Request, call_next):path = request.scope['path']response = await call_next(request)if path == "/":response_body = ""async for chunk in response.body_iterator:response_body += chunk.decode()some_javascript = f"""<script type="text/javascript" defer>{file_contents}</script>"""response_body = response_body.replace("</body>", some_javascript + "</body>")del response.headers["content-length"]return Response(content=response_body,status_code=response.status_code,headers=dict(response.headers),media_type=response.media_type)return responsedef candidate_to_json_string(arr):if isinstance(arr, list):a = []for item in arr:if len(item) >= 2:x, y = item[0], item[1]a.append(f'[{float(x):.2f}, {float(y):.2f}]')return '[' + ', '.join(a) + ']'return '[]'def subset_to_json_string(arr):if isinstance(arr, np.ndarray):arr_str = ','.join(['[' + ','.join([f'{num:.2f}' for num in row]) + ']' for row in arr])return '[' + arr_str + ']'elif isinstance(arr, list):arr_str = ','.join(['[' + ','.join([f'{float(num):.2f}' for num in row]) + ']' for row in arr])return '[' + arr_str + ']'return '[]'def estimate_body(source):if source == None:return Nonecandidate, subset = body_estimation(pil2cv(source))return "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"def image_changed(image):if image == None:return "estimation", {}if 'openpose' in image.info:print("pose found")jsonText = image.info['openpose']jsonObj = js.loads(jsonText)subset = jsonObj['subset']return f"""{image.width}px x {image.height}px, {len(subset)} individual(s)""", jsonTextelse:print("pose not found")candidate, subset = body_estimation(pil2cv(image))jsonText = "{ \"candidate\": " + candidate_to_json_string(candidate) + ", \"subset\": " + subset_to_json_string(subset) + " }"return f"""{image.width}px x {image.height}px, {subset.shape[0]} individual(s)""", jsonTextdef generate_pose_from_text(prompt: str, use_llm: bool = True):"""텍스트 프롬프트로부터 포즈 생성"""if use_llm and FIREWORKS_API_KEY != "YOUR_API_KEY_HERE":pose_data = generate_pose_from_llm_sync(prompt)else:pose_data = generate_template_pose(prompt)candidate_str = candidate_to_json_string(pose_data['candidate'])subset_str = subset_to_json_string(pose_data['subset'])return "{ \"candidate\": " + candidate_str + ", \"subset\": " + subset_str + " }"# HTML with embedded JavaScript for canvashtml_text = f"""<div style="position: relative; width: 100%; height: 512px;"><canvas id="canvas" width="512" height="512" style="border: 1px solid #ccc;"></canvas></div><script type="text/javascript">// Canvas initialization{file_contents}// Helper functions for Gradio integrationwindow.updateCanvasSize = function(width, height) {{const canvas = document.getElementById('canvas');if (canvas) {{if (width) canvas.width = width;if (height) canvas.height = height;if (typeof resizeCanvas !== 'undefined') {{resizeCanvas(width, height);}}}}}};window.loadPoseData = function(jsonStr) {{if (typeof importPose !== 'undefined' && jsonStr) {{try {{const poseData = JSON.parse(jsonStr);importPose(poseData);}} catch(e) {{console.error('Error loading pose:', e);}}}}}};// Initialize on loaddocument.addEventListener('DOMContentLoaded', function() {{if (typeof initializeEditor !== 'undefined') {{initializeEditor();}}}});</script>"""# Gradio interfacewith gr.Blocks(css="""button { min-width: 80px; }.prompt-box { border: 2px solid #667eea; border-radius: 8px; padding: 10px; }.llm-status { color: #667eea; font-weight: bold; }#canvas { border: 1px solid #ddd; }""") as demo:gr.Markdown("""# 🎨 AI-Powered Pose Generator with LLM### Generate precise line art poses from text descriptions""")with gr.Row():with gr.Column(scale=1):width = gr.Slider(label="Width", minimum=512, maximum=1024, step=64, value=512, interactive=True)height = gr.Slider(label="Height", minimum=512, maximum=1024, step=64, value=512, interactive=True)# LLM Pose Generationwith gr.Accordion(label="🤖 AI Pose Generation", open=True):prompt_input = gr.Textbox(label="Describe the pose",placeholder="e.g., 'A person sitting cross-legged' or 'Someone running'",lines=3)with gr.Row():use_llm_checkbox = gr.Checkbox(label="Use Advanced LLM", value=True)llm_status = gr.Markdown("")with gr.Row():generate_btn = gr.Button("🎯 Generate Pose", variant="primary")refine_btn = gr.Button("✨ Refine Current", variant="secondary")refinement_prompt = gr.Textbox(label="Refinement instructions",placeholder="e.g., 'Raise the left arm higher'",lines=2,visible=False)gr.Examples(examples=["A person standing with arms raised in victory","Someone sitting at a desk typing","A dancer in arabesque position","A person doing yoga warrior pose","Someone crouching in ready position","A person walking casually"],inputs=prompt_input)# Image Estimationwith gr.Accordion(label="📸 Pose from Image", open=False):source = gr.Image(type="pil")estimationResult = gr.Markdown("estimation")with gr.Row():applySizeBtn = gr.Button("Apply size")replaceBtn = gr.Button("Replace")importBtn = gr.Button("Import")# JSON Datawith gr.Accordion(label="📋 Json Data", open=False):with gr.Row():replaceWithJsonBtn = gr.Button("Replace")importJsonBtn = gr.Button("Import")json = gr.JSON(label="Json")jsonSource = gr.Textbox(label="Json source", lines=10)# Helpwith gr.Accordion(label="📝 Help", open=False):gr.Markdown("""**Controls:**- Ctrl+Drag: Scale- Alt+Drag: Move- Shift+Drag: Rotate- Ctrl+Z: Undo- D+Click: Delete person- Q+Click: Cut limb""")with gr.Column(scale=2):html = gr.HTML(html_text)with gr.Row():saveBtn = gr.Button("💾 Save", size="sm")generation_status = gr.Markdown("Ready to generate poses...")# Hidden components for JS communicationcanvas_update = gr.Textbox(visible=False)pose_data = gr.Textbox(visible=False)# Event handlers - without _js parameterdef update_canvas_size(w, h):return f"{{\"width\": {w}, \"height\": {h}}}"width.change(fn=lambda w: update_canvas_size(w, None),inputs=[width],outputs=[canvas_update])height.change(fn=lambda h: update_canvas_size(None, h),inputs=[height],outputs=[canvas_update])source.change(fn=image_changed,inputs=[source],outputs=[estimationResult, json])applySizeBtn.click(fn=lambda x: (x.width, x.height) if x else (512, 512),inputs=[source],outputs=[width, height])def apply_pose_to_canvas(json_data):return js.dumps(json_data) if json_data else "{}"replaceBtn.click(fn=apply_pose_to_canvas,inputs=[json],outputs=[pose_data])importBtn.click(fn=apply_pose_to_canvas,inputs=[json],outputs=[pose_data])# LLM generationdef handle_generate(prompt, use_llm):if not prompt:return None, "⚠️ Please enter a pose description", Nonetry:status = "🔄 Generating pose with AI..." if use_llm else "🔄 Using template..."pose_json = generate_pose_from_text(prompt, use_llm)pose_dict = js.loads(pose_json)return pose_dict, "✅ Pose generated successfully!", pose_jsonexcept Exception as e:return None, f"❌ Error: {str(e)}", Nonegenerate_btn.click(fn=handle_generate,inputs=[prompt_input, use_llm_checkbox],outputs=[json, generation_status, pose_data])def toggle_refinement():return gr.update(visible=True)refine_btn.click(fn=toggle_refinement,outputs=[refinement_prompt])def handle_refine(current_json, refinement):if not current_json or not refinement:return current_json, "⚠️ Need current pose and refinement instructions", Nonetry:refined = refine_pose_with_llm(current_json, refinement)return refined, "✅ Pose refined!", js.dumps(refined)except Exception as e:return current_json, f"❌ Refinement error: {str(e)}", Nonerefinement_prompt.submit(fn=handle_refine,inputs=[json, refinement_prompt],outputs=[json, generation_status, pose_data])def save_current_pose():# This would normally interact with the canvasreturn {"message": "Use the canvas save function"}saveBtn.click(fn=save_current_pose,outputs=[json])jsonSource.change(fn=lambda x: js.loads(x) if x else {},inputs=[jsonSource],outputs=[json])replaceWithJsonBtn.click(fn=apply_pose_to_canvas,inputs=[json],outputs=[pose_data])importJsonBtn.click(fn=apply_pose_to_canvas,inputs=[json],outputs=[pose_data])# Check API key statusdef check_api_status():if FIREWORKS_API_KEY == "YOUR_API_KEY_HERE":return "⚠️ LLM API key not configured - using templates"return "✅ LLM ready"demo.load(fn=check_api_status, outputs=[llm_status])# Mount Gradio app to FastAPIgr.mount_gradio_app(app, demo, path="/")# Main entry pointif __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)1234567891011127 unmodified lines20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181193 unmodified lines1231241251261271281291301311321333 unmodified lines1371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981993 unmodified lines2032042052062072082092102112122136 unmodified lines220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461import gradio as grimport numpy as npimport jsonimport requestsimport osfrom typing import Dict, List, Tupleimport base64from PIL import Image, ImageDrawimport io# Fireworks AI configurationFIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY", "YOUR_API_KEY_HERE")7 unmodified lines"LEye": 15, "REar": 16, "LEar": 17}# Skeleton connections for drawingPOSE_CONNECTIONS = [("Neck", "RShoulder"), ("RShoulder", "RElbow"), ("RElbow", "RWrist"),("Neck", "LShoulder"), ("LShoulder", "LElbow"), ("LElbow", "LWrist"),("Neck", "Nose"), ("Nose", "REye"), ("Nose", "LEye"),("REye", "REar"), ("LEye", "LEar"),("Neck", "RHip"), ("RHip", "RKnee"), ("RKnee", "RAnkle"),("Neck", "LHip"), ("LHip", "LKnee"), ("LKnee", "LAnkle"),("RHip", "LHip")]# Pose templatesPOSE_TEMPLATES = {"서있기 (Standing)": {"Nose": [256, 80], "Neck": [256, 120],"RShoulder": [220, 140], "RElbow": [200, 220], "RWrist": [190, 300],"LShoulder": [292, 140], "LElbow": [312, 220], "LWrist": [322, 300],"RHip": [240, 280], "RKnee": [235, 380], "RAnkle": [230, 480],"LHip": [272, 280], "LKnee": [277, 380], "LAnkle": [282, 480],"REye": [246, 70], "LEye": [266, 70], "REar": [236, 75], "LEar": [276, 75]},"앉기 (Sitting)": {"Nose": [256, 120], "Neck": [256, 160],"RShoulder": [220, 180], "RElbow": [200, 240], "RWrist": [190, 300],"LShoulder": [292, 180], "LElbow": [312, 240], "LWrist": [322, 300],"RHip": [240, 320], "RKnee": [280, 380], "RAnkle": [320, 400],"LHip": [272, 320], "LKnee": [232, 380], "LAnkle": [192, 400],"REye": [246, 110], "LEye": [266, 110], "REar": [236, 115], "LEar": [276, 115]},"달리기 (Running)": {"Nose": [256, 80], "Neck": [256, 120],"RShoulder": [220, 140], "RElbow": [180, 180], "RWrist": [150, 140],"LShoulder": [292, 140], "LElbow": [332, 200], "LWrist": [362, 260],"RHip": [240, 280], "RKnee": [260, 380], "RAnkle": [290, 470],"LHip": [272, 280], "LKnee": [252, 360], "LAnkle": [222, 440],"REye": [246, 70], "LEye": [266, 70], "REar": [236, 75], "LEar": [276, 75]},"요가 (Yoga)": {"Nose": [256, 100], "Neck": [256, 140],"RShoulder": [200, 160], "RElbow": [150, 120], "RWrist": [100, 100],"LShoulder": [312, 160], "LElbow": [362, 120], "LWrist": [412, 100],"RHip": [240, 300], "RKnee": [220, 400], "RAnkle": [200, 480],"LHip": [272, 300], "LKnee": [292, 400], "LAnkle": [312, 480],"REye": [246, 90], "LEye": [266, 90], "REar": [236, 95], "LEar": [276, 95]},"춤추기 (Dancing)": {"Nose": [256, 80], "Neck": [256, 120],"RShoulder": [220, 140], "RElbow": [180, 120], "RWrist": [140, 100],"LShoulder": [292, 140], "LElbow": [332, 160], "LWrist": [372, 140],"RHip": [240, 280], "RKnee": [260, 380], "RAnkle": [250, 480],"LHip": [272, 280], "LKnee": [252, 380], "LAnkle": [262, 480],"REye": [246, 70], "LEye": [266, 70], "REar": [236, 75], "LEar": [276, 75]}}def draw_pose(keypoints: Dict, width: int = 512, height: int = 512) -> Image.Image:"""키포인트를 기반으로 포즈 이미지 그리기"""img = Image.new('RGB', (width, height), color='white')draw = ImageDraw.Draw(img)# Draw skeleton connectionsfor start, end in POSE_CONNECTIONS:if start in keypoints and end in keypoints:start_point = keypoints[start]end_point = keypoints[end]if start_point and end_point:draw.line([tuple(start_point), tuple(end_point)], fill='blue', width=3)# Draw keypointsfor part, point in keypoints.items():if point:x, y = pointradius = 5draw.ellipse([x-radius, y-radius, x+radius, y+radius], fill='red', outline='darkred')return imgdef generate_pose_from_llm(prompt: str) -> Dict:"""LLM을 사용하여 텍스트로부터 포즈 생성"""system_prompt = """You are an expert in generating human pose keypoints.Given a description, generate 18 keypoint coordinates for OpenPose.Canvas size: 512x512 pixelsKeypoints: Nose, Neck, RShoulder, RElbow, RWrist, LShoulder, LElbow, LWrist,RHip, RKnee, RAnkle, LHip, LKnee, LAnkle, REye, LEye, REar, LEarReturn ONLY a JSON object with keypoint names and [x, y] coordinates.Example: {"Nose": [256, 80], "Neck": [256, 120], ...}Ensure anatomically correct proportions and center the pose."""headers = {"Accept": "application/json",3 unmodified linespayload = {"model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507","max_tokens": 1024,"temperature": 0.3,"messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": f"Generate pose keypoints for: {prompt}"}]}3 unmodified linesdata = response.json()content = data['choices'][0]['message']['content']# Extract JSON from responseimport rejson_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:keypoints = json.loads(json_match.group())return keypointsexcept Exception as e:print(f"LLM Error: {e}")# Fallback to templatereturn get_template_from_prompt(prompt)def get_template_from_prompt(prompt: str) -> Dict:"""프롬프트에서 키워드를 찾아 적절한 템플릿 선택"""prompt_lower = prompt.lower()if any(word in prompt_lower for word in ["앉", "sit", "chair", "의자"]):return POSE_TEMPLATES["앉기 (Sitting)"]elif any(word in prompt_lower for word in ["달리", "run", "jog", "뛰"]):return POSE_TEMPLATES["달리기 (Running)"]elif any(word in prompt_lower for word in ["요가", "yoga", "명상", "meditation"]):return POSE_TEMPLATES["요가 (Yoga)"]elif any(word in prompt_lower for word in ["춤", "dance", "댄스"]):return POSE_TEMPLATES["춤추기 (Dancing)"]else:return POSE_TEMPLATES["서있기 (Standing)"]def refine_pose(current_keypoints: Dict, instruction: str) -> Dict:"""기존 포즈를 지시사항에 따라 수정"""if FIREWORKS_API_KEY == "YOUR_API_KEY_HERE":# Simple rule-based refinementkeypoints = current_keypoints.copy()instruction_lower = instruction.lower()if "팔" in instruction_lower or "arm" in instruction_lower:if "올리" in instruction_lower or "raise" in instruction_lower:# Raise armsif "RWrist" in keypoints:keypoints["RWrist"][1] -= 50if "LWrist" in keypoints:keypoints["LWrist"][1] -= 50elif "내리" in instruction_lower or "lower" in instruction_lower:# Lower armsif "RWrist" in keypoints:keypoints["RWrist"][1] += 50if "LWrist" in keypoints:keypoints["LWrist"][1] += 50return keypoints# Use LLM for refinementsystem_prompt = """Modify the given pose keypoints based on the instruction.Return the modified keypoints in the same JSON format."""headers = {"Accept": "application/json",3 unmodified linespayload = {"model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507","max_tokens": 1024,"temperature": 0.2,"messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": f"Current keypoints: {json.dumps(current_keypoints)}\nInstruction: {instruction}"}]}6 unmodified linesimport rejson_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:return json.loads(json_match.group())except Exception as e:print(f"Refinement error: {e}")return current_keypointsdef keypoints_to_openpose_format(keypoints: Dict) -> str:"""키포인트를 OpenPose JSON 형식으로 변환"""candidate = []for i in range(18):part_name = Nonefor name, idx in BODY_PARTS.items():if idx == i:part_name = namebreakif part_name and part_name in keypoints:x, y = keypoints[part_name]candidate.append([float(x), float(y), 1.0])else:candidate.append([0.0, 0.0, 0.0])subset = [[i for i in range(18) if candidate[i][2] > 0]]subset[0].extend([float(len(subset[0])), len(subset[0])])return json.dumps({"candidate": candidate, "subset": subset}, indent=2)# Gradio Interfacewith gr.Blocks(title="AI Pose Generator", theme=gr.themes.Soft()) as demo:current_keypoints = gr.State({})gr.Markdown("""# 🎨 AI 포즈 생성기 (Line Art Pose Generator)### 텍스트 설명으로 정확한 포즈를 생성합니다""")with gr.Tabs():with gr.TabItem("🤖 AI 포즈 생성"):with gr.Row():with gr.Column(scale=1):# LLM 설정use_llm = gr.Checkbox(label="🚀 고급 AI 모델 사용 (Fireworks API)",value=False,info="체크하면 더 정확한 포즈 생성 (API 키 필요)")api_status = gr.Markdown("⚠️ API 키 미설정 - 템플릿 모드")# 텍스트 입력prompt = gr.Textbox(label="포즈 설명",placeholder="예: 의자에 앉아 책을 읽는 사람",lines=3)# 예제gr.Examples(examples=["팔을 높이 들고 승리의 포즈","의자에 앉아 노트북 타이핑","한쪽 다리로 서서 요가 포즈","양손을 허리에 올린 자신감 있는 포즈","달리기 자세","무릎 꿇고 기도하는 자세"],inputs=prompt)generate_btn = gr.Button("🎯 포즈 생성", variant="primary", size="lg")# 템플릿 선택with gr.Accordion("📚 템플릿 선택", open=False):template_select = gr.Dropdown(choices=list(POSE_TEMPLATES.keys()),label="포즈 템플릿",value="서있기 (Standing)")use_template_btn = gr.Button("템플릿 적용")with gr.Column(scale=1):# 포즈 이미지 출력pose_image = gr.Image(label="생성된 포즈",type="pil",height=512)# JSON 출력with gr.Accordion("📋 OpenPose JSON", open=False):json_output = gr.Code(label="JSON 데이터",language="json",lines=10)download_btn = gr.Button("💾 JSON 다운로드", size="sm")with gr.TabItem("✏️ 포즈 수정"):with gr.Row():with gr.Column():refinement_instruction = gr.Textbox(label="수정 지시사항",placeholder="예: 왼쪽 팔을 더 높이 들어주세요",lines=2)refine_btn = gr.Button("✨ 포즈 수정", variant="secondary")# 미세 조정with gr.Accordion("🎛️ 수동 조정", open=False):selected_part = gr.Dropdown(choices=list(BODY_PARTS.keys()),label="조정할 부위",value="RWrist")x_adjust = gr.Slider(-50, 50, 0, label="X 조정")y_adjust = gr.Slider(-50, 50, 0, label="Y 조정")apply_adjust_btn = gr.Button("적용")with gr.Column():refined_image = gr.Image(label="수정된 포즈",type="pil",height=512)with gr.TabItem("ℹ️ 사용법"):gr.Markdown("""## 사용 방법### 1. AI 포즈 생성- **텍스트 설명**: 원하는 포즈를 자연어로 설명하세요- **고급 AI 모델**: Fireworks API 키가 있으면 더 정확한 생성 가능- **템플릿**: 빠른 시작을 위한 기본 포즈 제공### 2. 포즈 수정- **자연어 수정**: "팔을 올려주세요" 같은 지시로 수정- **수동 조정**: 특정 관절을 직접 이동### 3. 내보내기- OpenPose 형식 JSON으로 다운로드- ControlNet 등에서 사용 가능### API 키 설정 (선택사항)```bashexport FIREWORKS_API_KEY="your_api_key"```### 특징- 🚀 GPU 불필요 - CPU만으로 작동- 🎨 깔끔한 라인 아트 스타일- 📊 OpenPose 호환 형식- 🔧 쉬운 수정 도구""")# Event handlersdef check_api_status():if FIREWORKS_API_KEY != "YOUR_API_KEY_HERE":return "✅ API 키 설정됨 - 고급 AI 사용 가능"return "⚠️ API 키 미설정 - 템플릿 모드"def generate_pose(prompt_text, use_llm_flag):if not prompt_text and not use_llm_flag:# Use default templatekeypoints = POSE_TEMPLATES["서있기 (Standing)"]elif use_llm_flag and FIREWORKS_API_KEY != "YOUR_API_KEY_HERE":keypoints = generate_pose_from_llm(prompt_text)else:keypoints = get_template_from_prompt(prompt_text)# Draw posepose_img = draw_pose(keypoints)json_str = keypoints_to_openpose_format(keypoints)return pose_img, json_str, keypointsdef use_template(template_name):keypoints = POSE_TEMPLATES[template_name]pose_img = draw_pose(keypoints)json_str = keypoints_to_openpose_format(keypoints)return pose_img, json_str, keypointsdef refine_existing_pose(instruction, keypoints_state):if not keypoints_state:return Nonerefined_keypoints = refine_pose(keypoints_state, instruction)pose_img = draw_pose(refined_keypoints)return pose_img, refined_keypointsdef manual_adjust(part, x_adj, y_adj, keypoints_state):if not keypoints_state or part not in keypoints_state:return None, keypoints_stateadjusted_keypoints = keypoints_state.copy()adjusted_keypoints[part][0] += x_adjadjusted_keypoints[part][1] += y_adjpose_img = draw_pose(adjusted_keypoints)return pose_img, adjusted_keypoints# Connect eventsdemo.load(check_api_status, outputs=api_status)generate_btn.click(generate_pose,inputs=[prompt, use_llm],outputs=[pose_image, json_output, current_keypoints])use_template_btn.click(use_template,inputs=[template_select],outputs=[pose_image, json_output, current_keypoints])refine_btn.click(refine_existing_pose,inputs=[refinement_instruction, current_keypoints],outputs=[refined_image, current_keypoints])apply_adjust_btn.click(manual_adjust,inputs=[selected_part, x_adjust, y_adjust, current_keypoints],outputs=[refined_image, current_keypoints])# Launchif __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,share=False,show_api=False)
app.py+336-436