Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -103,94 +103,16 @@ def process_speech(input_language, audio_input):
|
|
| 103 |
except Exception as e :
|
| 104 |
return f"{e}"
|
| 105 |
|
| 106 |
-
def decode_image(encoded_image: str) -> Image:
|
| 107 |
-
decoded_bytes = base64.b64decode(encoded_image.encode("utf-8"))
|
| 108 |
-
buffer = io.BytesIO(decoded_bytes)
|
| 109 |
-
image = Image.open(buffer)
|
| 110 |
-
return image
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
def encode_image(image: Image.Image, format: str = "PNG") -> str:
|
| 114 |
-
with io.BytesIO() as buffer:
|
| 115 |
-
image.save(buffer, format=format)
|
| 116 |
-
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 117 |
-
return encoded_image
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def get_conv_log_filename():
|
| 121 |
-
t = datetime.datetime.now()
|
| 122 |
-
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-conv.json")
|
| 123 |
-
return name
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
def get_conv_image_dir():
|
| 127 |
-
name = os.path.join(LOGDIR, "images")
|
| 128 |
-
os.makedirs(name, exist_ok=True)
|
| 129 |
-
return name
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def get_image_name(image, image_dir=None):
|
| 133 |
-
buffer = io.BytesIO()
|
| 134 |
-
image.save(buffer, format="PNG")
|
| 135 |
-
image_bytes = buffer.getvalue()
|
| 136 |
-
md5 = hashlib.md5(image_bytes).hexdigest()
|
| 137 |
-
|
| 138 |
-
if image_dir is not None:
|
| 139 |
-
image_name = os.path.join(image_dir, md5 + ".png")
|
| 140 |
-
else:
|
| 141 |
-
image_name = md5 + ".png"
|
| 142 |
-
|
| 143 |
-
return image_name
|
| 144 |
-
|
| 145 |
-
def resize_image(image, max_size):
|
| 146 |
-
width, height = image.size
|
| 147 |
-
aspect_ratio = float(width) / float(height)
|
| 148 |
-
|
| 149 |
-
if width > height:
|
| 150 |
-
new_width = max_size
|
| 151 |
-
new_height = int(new_width / aspect_ratio)
|
| 152 |
-
else:
|
| 153 |
-
new_height = max_size
|
| 154 |
-
new_width = int(new_height * aspect_ratio)
|
| 155 |
-
|
| 156 |
-
resized_image = image.resize((new_width, new_height))
|
| 157 |
-
return resized_image
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
def process_image(image_input):
|
| 162 |
# Initialize the Gradio client with the URL of the Gradio server
|
| 163 |
client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/pqjvl/")
|
| 164 |
|
| 165 |
-
#
|
| 166 |
-
|
| 167 |
-
# Convert the NumPy array to a PIL Image
|
| 168 |
-
image = Image.fromarray(image_input)
|
| 169 |
-
# Save the PIL Image to a temporary file
|
| 170 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
| 171 |
-
image.save(tmp_file.name)
|
| 172 |
-
image_path = tmp_file.name
|
| 173 |
-
elif isinstance(image_input, str):
|
| 174 |
-
try:
|
| 175 |
-
# Try to decode if it's a base64 string
|
| 176 |
-
image = decode_image(image_input)
|
| 177 |
-
except Exception:
|
| 178 |
-
# If decoding fails, assume it's a file path or a URL
|
| 179 |
-
image_path = image_input
|
| 180 |
-
else:
|
| 181 |
-
# If decoding succeeds, save the decoded image to a temporary file
|
| 182 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
| 183 |
-
image.save(tmp_file.name)
|
| 184 |
-
image_path = tmp_file.name
|
| 185 |
-
else:
|
| 186 |
-
# Assuming it's a PIL Image, save it to a temporary file
|
| 187 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
| 188 |
-
image_input.save(tmp_file.name)
|
| 189 |
-
image_path = tmp_file.name
|
| 190 |
|
| 191 |
# Call the predict method of the client
|
| 192 |
result = client.predict(
|
| 193 |
-
image_path, #
|
| 194 |
True, # Additional parameter for the server (e.g., enable detailed captioning)
|
| 195 |
fn_index=2 # Function index if the server has multiple functions
|
| 196 |
)
|
|
|
|
| 103 |
except Exception as e :
|
| 104 |
return f"{e}"
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def process_image(image_input):
|
| 107 |
# Initialize the Gradio client with the URL of the Gradio server
|
| 108 |
client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/pqjvl/")
|
| 109 |
|
| 110 |
+
# Assuming image_input is a URL path to the image
|
| 111 |
+
image_path = image_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Call the predict method of the client
|
| 114 |
result = client.predict(
|
| 115 |
+
image_path, # URL of the image
|
| 116 |
True, # Additional parameter for the server (e.g., enable detailed captioning)
|
| 117 |
fn_index=2 # Function index if the server has multiple functions
|
| 118 |
)
|