Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -94,15 +94,14 @@
|
|
94 |
# content={"error": "Method not allowed. Please check the API documentation."}
|
95 |
# )
|
96 |
|
97 |
-
|
98 |
# app.py
|
99 |
import os
|
100 |
import logging
|
|
|
101 |
from fastapi import FastAPI, HTTPException
|
102 |
from fastapi.responses import JSONResponse
|
103 |
from pydantic import BaseModel
|
104 |
from huggingface_hub import InferenceClient
|
105 |
-
from typing import Optional
|
106 |
|
107 |
# Set up logging
|
108 |
logging.basicConfig(level=logging.INFO)
|
@@ -111,8 +110,8 @@ logger = logging.getLogger(__name__)
|
|
111 |
# Initialize FastAPI app
|
112 |
app = FastAPI(
|
113 |
title="LLM Chat API",
|
114 |
-
description="API for getting chat responses from Llama model
|
115 |
-
version="1.
|
116 |
)
|
117 |
|
118 |
class ChatRequest(BaseModel):
|
@@ -130,64 +129,39 @@ def llm_chat_response(text: str, image_url: Optional[str] = None) -> str:
|
|
130 |
if not HF_TOKEN:
|
131 |
logger.error("HF_TOKEN not found in environment variables")
|
132 |
raise HTTPException(status_code=500, detail="HF_TOKEN not configured")
|
133 |
-
|
134 |
logger.info("Initializing InferenceClient...")
|
135 |
client = InferenceClient(
|
136 |
provider="sambanova",
|
137 |
api_key=HF_TOKEN
|
138 |
)
|
139 |
-
|
140 |
-
#
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
# Add image to content if provided
|
149 |
if image_url:
|
150 |
-
|
151 |
-
content.append({
|
152 |
"type": "image_url",
|
153 |
-
"image_url": {
|
154 |
-
"url": image_url
|
155 |
-
}
|
156 |
})
|
157 |
-
|
158 |
-
messages = [
|
159 |
-
{
|
160 |
-
"role": "user",
|
161 |
-
"content": content
|
162 |
-
}
|
163 |
-
]
|
164 |
-
|
165 |
-
logger.info("Sending request to model...")
|
166 |
-
logger.info(f"Request payload: {messages}")
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
completion = client.chat.completions.create(
|
169 |
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
170 |
messages=messages,
|
171 |
max_tokens=500
|
172 |
)
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
# Check the structure of the response and extract content
|
177 |
-
if hasattr(completion, 'choices') and len(completion.choices) > 0:
|
178 |
-
message = completion.choices[0].message
|
179 |
-
# Handle different response formats
|
180 |
-
if isinstance(message, dict) and 'content' in message:
|
181 |
-
return message['content']
|
182 |
-
elif hasattr(message, 'content'):
|
183 |
-
return message.content
|
184 |
-
else:
|
185 |
-
logger.error(f"Unexpected message format: {message}")
|
186 |
-
return str(message)
|
187 |
-
else:
|
188 |
-
logger.error(f"Unexpected completion format: {completion}")
|
189 |
-
return str(completion)
|
190 |
-
|
191 |
except Exception as e:
|
192 |
logger.error(f"Error in llm_chat_response: {str(e)}")
|
193 |
raise HTTPException(status_code=500, detail=str(e))
|
@@ -197,8 +171,7 @@ async def chat(request: ChatRequest):
|
|
197 |
try:
|
198 |
logger.info(f"Received chat request with text: {request.text}")
|
199 |
if request.image_url:
|
200 |
-
logger.info(f"Image URL
|
201 |
-
|
202 |
response = llm_chat_response(request.text, request.image_url)
|
203 |
return ChatResponse(response=response, status="success")
|
204 |
except HTTPException as he:
|
@@ -210,7 +183,7 @@ async def chat(request: ChatRequest):
|
|
210 |
|
211 |
@app.get("/")
|
212 |
async def root():
|
213 |
-
return {"message": "Welcome to the LLM Chat API
|
214 |
|
215 |
@app.exception_handler(404)
|
216 |
async def not_found_handler(request, exc):
|
@@ -224,4 +197,4 @@ async def method_not_allowed_handler(request, exc):
|
|
224 |
return JSONResponse(
|
225 |
status_code=405,
|
226 |
content={"error": "Method not allowed. Please check the API documentation."}
|
227 |
-
)
|
|
|
94 |
# content={"error": "Method not allowed. Please check the API documentation."}
|
95 |
# )
|
96 |
|
|
|
97 |
# app.py
|
98 |
import os
|
99 |
import logging
|
100 |
+
from typing import Optional
|
101 |
from fastapi import FastAPI, HTTPException
|
102 |
from fastapi.responses import JSONResponse
|
103 |
from pydantic import BaseModel
|
104 |
from huggingface_hub import InferenceClient
|
|
|
105 |
|
106 |
# Set up logging
|
107 |
logging.basicConfig(level=logging.INFO)
|
|
|
110 |
# Initialize FastAPI app
|
111 |
app = FastAPI(
|
112 |
title="LLM Chat API",
|
113 |
+
description="API for getting chat responses from Llama model (supports text and image input)",
|
114 |
+
version="1.0.0"
|
115 |
)
|
116 |
|
117 |
class ChatRequest(BaseModel):
|
|
|
129 |
if not HF_TOKEN:
|
130 |
logger.error("HF_TOKEN not found in environment variables")
|
131 |
raise HTTPException(status_code=500, detail="HF_TOKEN not configured")
|
132 |
+
|
133 |
logger.info("Initializing InferenceClient...")
|
134 |
client = InferenceClient(
|
135 |
provider="sambanova",
|
136 |
api_key=HF_TOKEN
|
137 |
)
|
138 |
+
|
139 |
+
# Build the messages payload dynamically based on whether an image URL is provided.
|
140 |
+
# If only text is provided, add an instruction for a one-line description.
|
141 |
+
message_content = [{
|
142 |
+
"type": "text",
|
143 |
+
"text": text + ("" if image_url else " describe in one line only")
|
144 |
+
}]
|
145 |
+
|
|
|
|
|
146 |
if image_url:
|
147 |
+
message_content.append({
|
|
|
148 |
"type": "image_url",
|
149 |
+
"image_url": {"url": image_url}
|
|
|
|
|
150 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
+
messages = [{
|
153 |
+
"role": "user",
|
154 |
+
"content": message_content
|
155 |
+
}]
|
156 |
+
|
157 |
+
logger.info("Sending request to model...")
|
158 |
completion = client.chat.completions.create(
|
159 |
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
160 |
messages=messages,
|
161 |
max_tokens=500
|
162 |
)
|
163 |
+
return completion.choices[0].message['content']
|
164 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
except Exception as e:
|
166 |
logger.error(f"Error in llm_chat_response: {str(e)}")
|
167 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
171 |
try:
|
172 |
logger.info(f"Received chat request with text: {request.text}")
|
173 |
if request.image_url:
|
174 |
+
logger.info(f"Image URL provided: {request.image_url}")
|
|
|
175 |
response = llm_chat_response(request.text, request.image_url)
|
176 |
return ChatResponse(response=response, status="success")
|
177 |
except HTTPException as he:
|
|
|
183 |
|
184 |
@app.get("/")
|
185 |
async def root():
|
186 |
+
return {"message": "Welcome to the LLM Chat API. Use POST /chat endpoint with 'text' and optionally 'image_url' for queries."}
|
187 |
|
188 |
@app.exception_handler(404)
|
189 |
async def not_found_handler(request, exc):
|
|
|
197 |
return JSONResponse(
|
198 |
status_code=405,
|
199 |
content={"error": "Method not allowed. Please check the API documentation."}
|
200 |
+
)
|