Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import re
|
| 3 |
import shutil
|
| 4 |
import base64
|
| 5 |
import tempfile
|
|
@@ -42,6 +41,7 @@ def ezgif_convert(media_bytes: bytes, filename: str = "input"):
|
|
| 42 |
files = {"new-image": (filename, media_bytes)}
|
| 43 |
r = requests.post("https://s.ezgif.com/upload", files=files, timeout=60)
|
| 44 |
r.raise_for_status()
|
|
|
|
| 45 |
m = re.search(r'name="file" value="([^"]+)"', r.text)
|
| 46 |
if not m:
|
| 47 |
raise RuntimeError("ezgif upload failed")
|
|
@@ -81,16 +81,7 @@ def convert_to_jpeg_bytes(media_bytes: bytes, filename_hint: str = "input"):
|
|
| 81 |
def to_b64_jpeg(img_bytes: bytes):
|
| 82 |
return base64.b64encode(img_bytes).decode("utf-8")
|
| 83 |
|
| 84 |
-
def
|
| 85 |
-
if custom and custom.strip():
|
| 86 |
-
return custom.strip()
|
| 87 |
-
if use_auto:
|
| 88 |
-
return ("Provide an exhaustive, highly detailed visual description suitable as an image-generation prompt. "
|
| 89 |
-
"Include objects, materials, textures, colors, lighting, viewpoint, camera lens and settings, "
|
| 90 |
-
"poses, facial expressions, clothing, background elements, small distinguishing details, mood, and composition.")
|
| 91 |
-
return "Provide a detailed description of this image."
|
| 92 |
-
|
| 93 |
-
def generate_stream(image_src: str, use_auto: bool, custom_prompt: str, alt_key: str):
|
| 94 |
try:
|
| 95 |
raw = fetch_bytes(image_src)
|
| 96 |
jpg = convert_to_jpeg_bytes(raw, filename_hint=os.path.basename(image_src) or "input")
|
|
@@ -99,12 +90,13 @@ def generate_stream(image_src: str, use_auto: bool, custom_prompt: str, alt_key:
|
|
| 99 |
return
|
| 100 |
|
| 101 |
b64 = to_b64_jpeg(jpg)
|
| 102 |
-
|
|
|
|
| 103 |
model = "pixtral-12b-2409"
|
| 104 |
messages = [{
|
| 105 |
"role": "user",
|
| 106 |
"content": [
|
| 107 |
-
{"type": "text", "text":
|
| 108 |
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64}"}
|
| 109 |
],
|
| 110 |
"stream": False
|
|
@@ -121,18 +113,17 @@ def generate_stream(image_src: str, use_auto: bool, custom_prompt: str, alt_key:
|
|
| 121 |
yield f"Model error: {e}"
|
| 122 |
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
-
gr.Markdown("Image
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
alt_key = gr.Textbox(label="Alternate Mistral API Key (optional)", type="password")
|
| 129 |
-
url_input = gr.Textbox(label="Image URL", placeholder="https://...")
|
| 130 |
-
custom = gr.Textbox(label="Custom prompt (optional)", lines=4, placeholder="
|
| 131 |
-
auto_toggle = gr.Checkbox(label="Use detailed auto prompt", value=True)
|
| 132 |
-
preview = gr.Image(label="Preview", type="pil")
|
| 133 |
submit = gr.Button("Submit")
|
|
|
|
| 134 |
with gr.Column(scale=1):
|
| 135 |
-
out = gr.Textbox(label="Generated Detailed Description", lines=
|
| 136 |
|
| 137 |
def load_preview(url):
|
| 138 |
if not url:
|
|
@@ -145,13 +136,13 @@ with gr.Blocks() as demo:
|
|
| 145 |
except Exception:
|
| 146 |
return None
|
| 147 |
|
| 148 |
-
def start_gen(url,
|
| 149 |
if not url:
|
| 150 |
return "No URL provided."
|
| 151 |
-
for chunk in generate_stream(url,
|
| 152 |
yield chunk
|
| 153 |
|
| 154 |
url_input.change(fn=load_preview, inputs=[url_input], outputs=[preview])
|
| 155 |
-
submit.click(fn=start_gen, inputs=[url_input,
|
| 156 |
|
| 157 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import shutil
|
| 3 |
import base64
|
| 4 |
import tempfile
|
|
|
|
| 41 |
files = {"new-image": (filename, media_bytes)}
|
| 42 |
r = requests.post("https://s.ezgif.com/upload", files=files, timeout=60)
|
| 43 |
r.raise_for_status()
|
| 44 |
+
import re
|
| 45 |
m = re.search(r'name="file" value="([^"]+)"', r.text)
|
| 46 |
if not m:
|
| 47 |
raise RuntimeError("ezgif upload failed")
|
|
|
|
| 81 |
def to_b64_jpeg(img_bytes: bytes):
|
| 82 |
return base64.b64encode(img_bytes).decode("utf-8")
|
| 83 |
|
| 84 |
+
def generate_stream(image_src: str, custom_prompt: str, alt_key: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
try:
|
| 86 |
raw = fetch_bytes(image_src)
|
| 87 |
jpg = convert_to_jpeg_bytes(raw, filename_hint=os.path.basename(image_src) or "input")
|
|
|
|
| 90 |
return
|
| 91 |
|
| 92 |
b64 = to_b64_jpeg(jpg)
|
| 93 |
+
prompt_text = (custom_prompt.strip() if custom_prompt and custom_prompt.strip() else
|
| 94 |
+
"Provide a detailed, neutral, clinical-style description of the image focusing on observable non-sexual features, hygiene, skin condition, posture, and general anatomy. Keep language professional.")
|
| 95 |
model = "pixtral-12b-2409"
|
| 96 |
messages = [{
|
| 97 |
"role": "user",
|
| 98 |
"content": [
|
| 99 |
+
{"type": "text", "text": prompt_text},
|
| 100 |
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64}"}
|
| 101 |
],
|
| 102 |
"stream": False
|
|
|
|
| 113 |
yield f"Model error: {e}"
|
| 114 |
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown("Image/Video to Clinical Description (custom prompt optional)")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
alt_key = gr.Textbox(label="Alternate Mistral API Key (optional)", type="password")
|
| 121 |
+
url_input = gr.Textbox(label="Image/Video URL", placeholder="https://...")
|
| 122 |
+
custom = gr.Textbox(label="Custom prompt (optional)", lines=4, placeholder="Enter custom prompt to override default")
|
|
|
|
|
|
|
| 123 |
submit = gr.Button("Submit")
|
| 124 |
+
preview = gr.Image(label="Preview", type="pil")
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
+
out = gr.Textbox(label="Generated Detailed Description", lines=25)
|
| 127 |
|
| 128 |
def load_preview(url):
|
| 129 |
if not url:
|
|
|
|
| 136 |
except Exception:
|
| 137 |
return None
|
| 138 |
|
| 139 |
+
def start_gen(url, custom_p, alt_k):
|
| 140 |
if not url:
|
| 141 |
return "No URL provided."
|
| 142 |
+
for chunk in generate_stream(url, custom_p, alt_k):
|
| 143 |
yield chunk
|
| 144 |
|
| 145 |
url_input.change(fn=load_preview, inputs=[url_input], outputs=[preview])
|
| 146 |
+
submit.click(fn=start_gen, inputs=[url_input, custom, alt_key], outputs=[out])
|
| 147 |
|
| 148 |
demo.launch()
|