Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,15 +9,20 @@ def image_to_data_url(image_path):
|
|
| 9 |
return None
|
| 10 |
with Image.open(image_path) as img:
|
| 11 |
buffered = io.BytesIO()
|
| 12 |
-
img.
|
|
|
|
| 13 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 14 |
-
return f"data:image/{
|
| 15 |
|
| 16 |
def process_input(image, image_url, prompt, model, hf_token):
|
| 17 |
if not hf_token.startswith("hf_"):
|
| 18 |
raise gr.Error("Invalid Hugging Face token. It should start with 'hf_'")
|
| 19 |
|
| 20 |
-
client = InferenceClient(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
image_data = None
|
| 23 |
if image is not None:
|
|
@@ -38,7 +43,6 @@ def process_input(image, image_url, prompt, model, hf_token):
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
stream = client.chat.completions.create(
|
| 41 |
-
model=model,
|
| 42 |
messages=messages,
|
| 43 |
max_tokens=512,
|
| 44 |
stream=True,
|
|
@@ -46,9 +50,14 @@ def process_input(image, image_url, prompt, model, hf_token):
|
|
| 46 |
|
| 47 |
full_response = ""
|
| 48 |
for chunk in stream:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
raise gr.Error(f"API Error: {str(e)}")
|
| 54 |
|
|
@@ -63,7 +72,6 @@ with gr.Blocks() as demo:
|
|
| 63 |
|
| 64 |
*Explore state-of-the-art vision-language models by Cohere through this interface.
|
| 65 |
Supports image inputs via upload or URL, with streaming responses.*
|
| 66 |
-
|
| 67 |
Read more about Aya Vision [here](https://cohere.com/research/aya)
|
| 68 |
|
| 69 |
**Get your HF token:** [Hugging Face Settings](https://huggingface.co/settings/tokens)
|
|
@@ -140,4 +148,4 @@ with gr.Blocks() as demo:
|
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
-
demo.
|
|
|
|
| 9 |
return None
|
| 10 |
with Image.open(image_path) as img:
|
| 11 |
buffered = io.BytesIO()
|
| 12 |
+
img_format = img.format if img.format else "JPEG"
|
| 13 |
+
img.save(buffered, format=img_format)
|
| 14 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 15 |
+
return f"data:image/{img_format.lower()};base64,{img_str}"
|
| 16 |
|
| 17 |
def process_input(image, image_url, prompt, model, hf_token):
|
| 18 |
if not hf_token.startswith("hf_"):
|
| 19 |
raise gr.Error("Invalid Hugging Face token. It should start with 'hf_'")
|
| 20 |
|
| 21 |
+
client = InferenceClient(
|
| 22 |
+
model=model,
|
| 23 |
+
token=hf_token,
|
| 24 |
+
provider="cohere"
|
| 25 |
+
)
|
| 26 |
|
| 27 |
image_data = None
|
| 28 |
if image is not None:
|
|
|
|
| 43 |
|
| 44 |
try:
|
| 45 |
stream = client.chat.completions.create(
|
|
|
|
| 46 |
messages=messages,
|
| 47 |
max_tokens=512,
|
| 48 |
stream=True,
|
|
|
|
| 50 |
|
| 51 |
full_response = ""
|
| 52 |
for chunk in stream:
|
| 53 |
+
if hasattr(chunk.choices[0], 'delta') and hasattr(chunk.choices[0].delta, 'content'):
|
| 54 |
+
content = chunk.choices[0].delta.content or ""
|
| 55 |
+
full_response += content
|
| 56 |
+
yield full_response
|
| 57 |
+
elif hasattr(chunk, 'content'):
|
| 58 |
+
content = chunk.content or ""
|
| 59 |
+
full_response += content
|
| 60 |
+
yield full_response
|
| 61 |
except Exception as e:
|
| 62 |
raise gr.Error(f"API Error: {str(e)}")
|
| 63 |
|
|
|
|
| 72 |
|
| 73 |
*Explore state-of-the-art vision-language models by Cohere through this interface.
|
| 74 |
Supports image inputs via upload or URL, with streaming responses.*
|
|
|
|
| 75 |
Read more about Aya Vision [here](https://cohere.com/research/aya)
|
| 76 |
|
| 77 |
**Get your HF token:** [Hugging Face Settings](https://huggingface.co/settings/tokens)
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
demo.launch()
|