Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,67 +3,87 @@ import requests
|
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# llama.cpp server endpoint
|
| 7 |
LLAMA_URL = "http://localhost:8080/v1/chat/completions"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
{
|
| 14 |
-
"image": {
|
| 15 |
-
|
| 16 |
-
"style": "photorealistic/anime/etc",
|
| 17 |
-
"aspect_ratio": "16:9",
|
| 18 |
-
"lighting": "description"
|
| 19 |
-
},
|
| 20 |
-
"video": {
|
| 21 |
-
"prompt": "scene description with motion",
|
| 22 |
-
"motion": "camera movement details",
|
| 23 |
-
"duration": 5
|
| 24 |
-
}
|
| 25 |
}"""
|
| 26 |
|
| 27 |
-
def generate(user_input):
|
| 28 |
-
if not user_input.strip():
|
| 29 |
-
return
|
| 30 |
|
| 31 |
-
#
|
|
|
|
|
|
|
| 32 |
payload = {
|
| 33 |
"messages": [
|
| 34 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 35 |
-
{"role": "user", "content": user_input}
|
| 36 |
],
|
| 37 |
-
"temperature": 0.
|
| 38 |
-
"max_tokens":
|
| 39 |
}
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
response = requests.post(LLAMA_URL, json=payload, timeout=
|
| 43 |
content = response.json()["choices"][0]["message"]["content"].strip()
|
| 44 |
|
| 45 |
-
#
|
| 46 |
json_match = re.search(r'\{[\s\S]*\}', content)
|
| 47 |
if json_match:
|
| 48 |
return json.loads(json_match.group())
|
| 49 |
-
return {"error": "Model
|
| 50 |
except Exception as e:
|
| 51 |
-
return {"error": str(e)}
|
| 52 |
|
| 53 |
-
#
|
| 54 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", neutral_hue="slate")) as demo:
|
| 55 |
-
gr.
|
| 56 |
-
gr.
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column(scale=1):
|
| 60 |
-
input_text = gr.Textbox(
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
with gr.Column(scale=2):
|
| 64 |
-
output_json = gr.JSON(label="
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
|
|
|
|
| 6 |
LLAMA_URL = "http://localhost:8080/v1/chat/completions"
|
| 7 |
|
| 8 |
+
# Advanced System Prompt: Prevents repetition and forces layered details
|
| 9 |
+
SYSTEM_PROMPT = """You are PromptForge. Create hyper-detailed JSON prompts.
|
| 10 |
+
STRICT RULE: Never repeat a phrase or concept within the same prompt.
|
| 11 |
+
Each sentence must provide NEW information (texture, lighting, specific movement, or camera angle).
|
| 12 |
|
| 13 |
+
If 'Image' is active:
|
| 14 |
+
- Focus on the 'frozen moment': materials (brushed steel, translucent skin),
|
| 15 |
+
lighting (crepuscular rays, neon flicker), and composition.
|
| 16 |
+
|
| 17 |
+
If 'Video' is active:
|
| 18 |
+
- Focus on 'progression': How does the light change? What is the specific
|
| 19 |
+
physics of the motion? (e.g., 'the dragon's weight displaces the forest canopy').
|
| 20 |
+
|
| 21 |
+
Response must be ONLY valid JSON:
|
| 22 |
{
|
| 23 |
+
"image": { "prompt": "...", "style": "...", "aspect_ratio": "...", "lighting": "..." },
|
| 24 |
+
"video": { "prompt": "...", "motion": "...", "duration": 5 }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}"""
|
| 26 |
|
| 27 |
+
def generate(user_input, selected_modes):
|
| 28 |
+
if not user_input.strip() or not selected_modes:
|
| 29 |
+
return {"error": "Input text and at least one mode (Image/Video) required."}
|
| 30 |
|
| 31 |
+
# Instruct the model on which keys to generate
|
| 32 |
+
mode_context = f"Generate only for: {', '.join(selected_modes)}."
|
| 33 |
+
|
| 34 |
payload = {
|
| 35 |
"messages": [
|
| 36 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 37 |
+
{"role": "user", "content": f"Task: {user_input}\nContext: {mode_context}\nRequirement: High complexity, zero repetition, extreme sensory detail."}
|
| 38 |
],
|
| 39 |
+
"temperature": 0.45, # Slightly higher for creative vocabulary
|
| 40 |
+
"max_tokens": 1200
|
| 41 |
}
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
response = requests.post(LLAMA_URL, json=payload, timeout=90)
|
| 45 |
content = response.json()["choices"][0]["message"]["content"].strip()
|
| 46 |
|
| 47 |
+
# Robust JSON extraction
|
| 48 |
json_match = re.search(r'\{[\s\S]*\}', content)
|
| 49 |
if json_match:
|
| 50 |
return json.loads(json_match.group())
|
| 51 |
+
return {"error": "Model output was not valid JSON.", "raw_content": content}
|
| 52 |
except Exception as e:
|
| 53 |
+
return {"error": f"Connection Error: {str(e)}"}
|
| 54 |
|
| 55 |
+
# --- UI Setup ---
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", neutral_hue="slate")) as demo:
|
| 57 |
+
gr.HTML("<h1 style='text-align: center; margin-bottom: 0;'>⚡ PromptForge</h1>")
|
| 58 |
+
gr.HTML("<p style='text-align: center; color: #888;'>Press <b>Enter</b> or click <b>Forge</b> to generate</p>")
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=1):
|
| 62 |
+
input_text = gr.Textbox(
|
| 63 |
+
label="Core Idea",
|
| 64 |
+
placeholder="e.g. A cybernetic dragon in a forest...",
|
| 65 |
+
lines=3,
|
| 66 |
+
autofocus=True # Sets focus so you can type immediately
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Selection for Image/Video
|
| 70 |
+
modes = gr.CheckboxGroup(
|
| 71 |
+
choices=["Image", "Video"],
|
| 72 |
+
value=["Image", "Video"],
|
| 73 |
+
label="Prompt Types to Generate"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
run_btn = gr.Button("FORGE →", variant="primary")
|
| 77 |
+
|
| 78 |
with gr.Column(scale=2):
|
| 79 |
+
output_json = gr.JSON(label="Resulting JSON Data")
|
| 80 |
|
| 81 |
+
# --- THE MAGIC TRIGGERS ---
|
| 82 |
+
# 1. Clicking the button
|
| 83 |
+
run_btn.click(fn=generate, inputs=[input_text, modes], outputs=output_json)
|
| 84 |
+
|
| 85 |
+
# 2. Hitting ENTER on the keyboard
|
| 86 |
+
input_text.submit(fn=generate, inputs=[input_text, modes], outputs=output_json)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|