Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,24 @@ import re
|
|
| 6 |
from groq import Groq
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
# Read
|
| 10 |
api_key = os.getenv("GROQ_API_KEY", "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")
|
| 11 |
client = Groq(api_key=api_key)
|
| 12 |
|
| 13 |
-
def encode_image(
|
| 14 |
-
"""Encode
|
| 15 |
try:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# Resize the image to a height of 512 while maintaining aspect ratio
|
| 19 |
base_height = 512
|
| 20 |
h_percent = (base_height / float(image.size[1]))
|
| 21 |
w_size = int((float(image.size[0]) * float(h_percent)))
|
|
@@ -23,9 +31,7 @@ def encode_image(image_path):
|
|
| 23 |
|
| 24 |
buffered = BytesIO()
|
| 25 |
image.save(buffered, format="JPEG")
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return img_str
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Error encoding image: {e}")
|
| 31 |
return None
|
|
@@ -94,4 +100,5 @@ with gr.Blocks() as demo:
|
|
| 94 |
api_name="feifeichat"
|
| 95 |
)
|
| 96 |
|
|
|
|
| 97 |
demo.launch(ssr_mode=False)
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Read key from Hugging Face secrets or default fallback
|
| 10 |
api_key = os.getenv("GROQ_API_KEY", "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")
|
| 11 |
client = Groq(api_key=api_key)
|
| 12 |
|
| 13 |
+
def encode_image(image_input):
|
| 14 |
+
"""Encode image (path, dict, or base64 data URI) to base64 string."""
|
| 15 |
try:
|
| 16 |
+
# If Gradio passes a FileData dictionary
|
| 17 |
+
if isinstance(image_input, dict):
|
| 18 |
+
image_input = image_input.get("path", "")
|
| 19 |
+
|
| 20 |
+
# If already a base64 Data URI string
|
| 21 |
+
if isinstance(image_input, str) and image_input.startswith("data:image"):
|
| 22 |
+
return image_input.split(",")[1]
|
| 23 |
+
|
| 24 |
+
# Otherwise treat as a local file path and open via PIL
|
| 25 |
+
image = Image.open(image_input).convert("RGB")
|
| 26 |
|
|
|
|
| 27 |
base_height = 512
|
| 28 |
h_percent = (base_height / float(image.size[1]))
|
| 29 |
w_size = int((float(image.size[0]) * float(h_percent)))
|
|
|
|
| 31 |
|
| 32 |
buffered = BytesIO()
|
| 33 |
image.save(buffered, format="JPEG")
|
| 34 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error encoding image: {e}")
|
| 37 |
return None
|
|
|
|
| 100 |
api_name="feifeichat"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Disabling SSR mode resolves SvelteKit 405 Method Not Allowed errors on Gradio 5
|
| 104 |
demo.launch(ssr_mode=False)
|