import gradio as gr import requests import json import re LLAMA_URL = "http://localhost:8080/v1/chat/completions" # Slimmed down for speed. We tell it WHAT to do, not HOW to think. SYSTEM_PROMPT = """You are PromptForge. Respond ONLY in JSON. Provide unique, non-repetitive descriptions for Image and Video. JSON Structure: { "image": { "prompt": "...", "style": "...", "aspect_ratio": "16:9", "lighting": "..." }, "video": { "prompt": "...", "motion": "...", "duration": 5 } }""" def generate(user_input, selected_modes): if not user_input.strip() or not selected_modes: return {"error": "Input and at least one mode required."} # Keep the user instruction short to save CPU cycles mode_str = ", ".join(selected_modes) user_message = f"Create highly detailed {mode_str} prompts for: {user_input}. No repetition." payload = { "messages": [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_message} ], "temperature": 0.3, # Balanced: Creative but fast "max_tokens": 800 # Lowered to prevent 'rambling' and timeouts } try: # Increased timeout to 120 just in case, but model should finish faster now response = requests.post(LLAMA_URL, json=payload, timeout=120) content = response.json()["choices"][0]["message"]["content"].strip() json_match = re.search(r'\{[\s\S]*\}', content) if json_match: return json.loads(json_match.group()) return {"error": "Invalid JSON output", "raw": content} except Exception as e: return {"error": f"Request failed: {str(e)}"} # --- UI Setup --- with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", neutral_hue="slate")) as demo: gr.HTML("