File size: 2,720 Bytes
9ce3351
 
 
 
 
 
 
9906841
 
 
9ce3351
9906841
9ce3351
9906841
4af54de
7f98ba3
 
4af54de
 
9906841
9ce3351
9906841
 
 
4af54de
7f98ba3
 
 
9906841
7f98ba3
9906841
 
7f98ba3
9ce3351
 
9906841
 
7f98ba3
 
 
9ce3351
7f98ba3
9906841
9ce3351
9906841
9ce3351
4af54de
7f98ba3
9906841
7f98ba3
9ce3351
7f98ba3
4af54de
 
9906841
4af54de
9906841
4af54de
 
 
 
9906841
4af54de
 
 
7f98ba3
9906841
9ce3351
9906841
4af54de
 
9ce3351
7f98ba3
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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("<h1 style='text-align: center;'>⚡ PromptForge</h1>")
    
    with gr.Row():
        with gr.Column(scale=1):
            input_text = gr.Textbox(
                label="Core Idea", 
                placeholder="Type and hit Enter...",
                lines=3,
                autofocus=True 
            )
            modes = gr.CheckboxGroup(
                choices=["Image", "Video"], 
                value=["Image", "Video"], 
                label="Generate For:"
            )
            run_btn = gr.Button("FORGE →", variant="primary")
            
        with gr.Column(scale=2):
            output_json = gr.JSON(label="JSON Data")

    # Magic Triggers
    run_btn.click(fn=generate, inputs=[input_text, modes], outputs=output_json)
    input_text.submit(fn=generate, inputs=[input_text, modes], outputs=output_json)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)