Spaces:
Runtime error
Runtime error
Update ClassPrompt.py
Browse files- ClassPrompt.py +16 -1
ClassPrompt.py
CHANGED
|
@@ -2,6 +2,9 @@ import random # Import the random module
|
|
| 2 |
from groq import Groq
|
| 3 |
from openai import OpenAI
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
class PromptClass:
|
| 6 |
def __init__(self):
|
| 7 |
self.huggingface_token = os.environ.get("HF_TOKEN")
|
|
@@ -303,4 +306,16 @@ Your output is only the caption itself, no comments or extra formatting. The cap
|
|
| 303 |
sentences = output.split(". ")
|
| 304 |
if len(sentences) > 1:
|
| 305 |
output = ". ".join(sentences[1:]).strip()
|
| 306 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
from openai import OpenAI
|
| 4 |
import os
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
from huggingface_hub import InferenceApi
|
| 8 |
class PromptClass:
|
| 9 |
def __init__(self):
|
| 10 |
self.huggingface_token = os.environ.get("HF_TOKEN")
|
|
|
|
| 306 |
sentences = output.split(". ")
|
| 307 |
if len(sentences) > 1:
|
| 308 |
output = ". ".join(sentences[1:]).strip()
|
| 309 |
+
return output
|
| 310 |
+
def img2text(self,image=None):
|
| 311 |
+
if image:
|
| 312 |
+
# Select the appropriate provider
|
| 313 |
+
inference = InferenceApi(repo_id="Salesforce/blip-image-captioning-base", token=self.huggingface_token)
|
| 314 |
+
# Đọc file hình ảnh
|
| 315 |
+
image_bytes = io.BytesIO()
|
| 316 |
+
image.save(image_bytes, format="JPEG")
|
| 317 |
+
image_data = image_bytes.getvalue()
|
| 318 |
+
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
| 319 |
+
# Gửi yêu cầu API
|
| 320 |
+
response = inference(inputs={"image":image_base64})
|
| 321 |
+
return response[0]["generated_text"]
|