Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -105,10 +105,8 @@ from huggingface_hub import InferenceClient
|
|
| 105 |
from pydantic import BaseModel
|
| 106 |
import base64
|
| 107 |
import logging
|
| 108 |
-
from typing import Optional
|
| 109 |
-
import uuid
|
| 110 |
from typing import Optional, ClassVar, List
|
| 111 |
-
|
| 112 |
|
| 113 |
# Set up logging
|
| 114 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -117,10 +115,10 @@ logger = logging.getLogger(__name__)
|
|
| 117 |
class TextImageRequest(BaseModel):
|
| 118 |
text: Optional[str] = None
|
| 119 |
image_base64: Optional[str] = None
|
| 120 |
-
voice: str = "af_heart" # Default voice
|
| 121 |
speed: float = 1.0
|
| 122 |
|
| 123 |
-
#
|
| 124 |
AVAILABLE_VOICES: ClassVar[List[str]] = ["af_heart"]
|
| 125 |
|
| 126 |
def validate_voice(self):
|
|
@@ -160,7 +158,7 @@ def llm_chat_response(text, image_base64=None):
|
|
| 160 |
|
| 161 |
logger.info("Initializing InferenceClient...")
|
| 162 |
client = InferenceClient(
|
| 163 |
-
provider="hf-inference", # Using correct provider
|
| 164 |
api_key=HF_TOKEN
|
| 165 |
)
|
| 166 |
|
|
@@ -180,20 +178,17 @@ def llm_chat_response(text, image_base64=None):
|
|
| 180 |
base_url = os.getenv("BASE_URL", "http://localhost:8000")
|
| 181 |
image_url = f"{base_url}/static/{filename}"
|
| 182 |
prompt = text if text else "Describe this image in one sentence."
|
|
|
|
| 183 |
messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
{
|
| 185 |
"role": "user",
|
| 186 |
"content": [
|
| 187 |
-
{
|
| 188 |
-
|
| 189 |
-
"text": prompt
|
| 190 |
-
},
|
| 191 |
-
{
|
| 192 |
-
"type": "image_url",
|
| 193 |
-
"image_url": {
|
| 194 |
-
"url": image_url
|
| 195 |
-
}
|
| 196 |
-
}
|
| 197 |
]
|
| 198 |
}
|
| 199 |
]
|
|
@@ -344,3 +339,4 @@ async def method_not_allowed_handler(request: Request, exc):
|
|
| 344 |
status_code=405,
|
| 345 |
content={"error": "Method not allowed. Please check the API documentation."}
|
| 346 |
)
|
|
|
|
|
|
| 105 |
from pydantic import BaseModel
|
| 106 |
import base64
|
| 107 |
import logging
|
|
|
|
|
|
|
| 108 |
from typing import Optional, ClassVar, List
|
| 109 |
+
import uuid
|
| 110 |
|
| 111 |
# Set up logging
|
| 112 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 115 |
class TextImageRequest(BaseModel):
|
| 116 |
text: Optional[str] = None
|
| 117 |
image_base64: Optional[str] = None
|
| 118 |
+
voice: str = "af_heart" # Default voice that we know exists
|
| 119 |
speed: float = 1.0
|
| 120 |
|
| 121 |
+
# Annotated as a ClassVar so Pydantic doesn't treat it as a model field.
|
| 122 |
AVAILABLE_VOICES: ClassVar[List[str]] = ["af_heart"]
|
| 123 |
|
| 124 |
def validate_voice(self):
|
|
|
|
| 158 |
|
| 159 |
logger.info("Initializing InferenceClient...")
|
| 160 |
client = InferenceClient(
|
| 161 |
+
provider="hf-inference", # Using the correct provider
|
| 162 |
api_key=HF_TOKEN
|
| 163 |
)
|
| 164 |
|
|
|
|
| 178 |
base_url = os.getenv("BASE_URL", "http://localhost:8000")
|
| 179 |
image_url = f"{base_url}/static/{filename}"
|
| 180 |
prompt = text if text else "Describe this image in one sentence."
|
| 181 |
+
# Include a system message to provide conversation context
|
| 182 |
messages = [
|
| 183 |
+
{
|
| 184 |
+
"role": "system",
|
| 185 |
+
"content": "You are a helpful assistant that describes images and answers questions about them."
|
| 186 |
+
},
|
| 187 |
{
|
| 188 |
"role": "user",
|
| 189 |
"content": [
|
| 190 |
+
{"type": "text", "text": prompt},
|
| 191 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
]
|
| 193 |
}
|
| 194 |
]
|
|
|
|
| 339 |
status_code=405,
|
| 340 |
content={"error": "Method not allowed. Please check the API documentation."}
|
| 341 |
)
|
| 342 |
+
|