Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
|
|
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from typing import Optional
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI(
|
| 10 |
title="LLM Chat API",
|
|
@@ -17,26 +22,32 @@ class ChatRequest(BaseModel):
|
|
| 17 |
|
| 18 |
class ChatResponse(BaseModel):
|
| 19 |
response: str
|
|
|
|
| 20 |
|
| 21 |
def llm_chat_response(text: str) -> str:
|
| 22 |
try:
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 24 |
if not HF_TOKEN:
|
|
|
|
| 25 |
raise HTTPException(status_code=500, detail="HF_TOKEN not configured")
|
| 26 |
|
|
|
|
| 27 |
client = InferenceClient(api_key=HF_TOKEN)
|
|
|
|
| 28 |
messages = [
|
| 29 |
{
|
| 30 |
"role": "user",
|
| 31 |
"content": [
|
| 32 |
{
|
| 33 |
"type": "text",
|
| 34 |
-
"text": text + str('describe in one line only')
|
| 35 |
}
|
| 36 |
]
|
| 37 |
}
|
| 38 |
]
|
| 39 |
|
|
|
|
| 40 |
response_from_llama = client.chat.completions.create(
|
| 41 |
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
| 42 |
messages=messages,
|
|
@@ -44,18 +55,31 @@ def llm_chat_response(text: str) -> str:
|
|
| 44 |
)
|
| 45 |
return response_from_llama.choices[0].message['content']
|
| 46 |
except Exception as e:
|
|
|
|
| 47 |
raise HTTPException(status_code=500, detail=str(e))
|
| 48 |
|
| 49 |
@app.post("/chat", response_model=ChatResponse)
|
| 50 |
async def chat(request: ChatRequest):
|
| 51 |
try:
|
|
|
|
| 52 |
response = llm_chat_response(request.text)
|
| 53 |
-
return ChatResponse(response=response)
|
| 54 |
except HTTPException as he:
|
|
|
|
| 55 |
raise he
|
| 56 |
except Exception as e:
|
|
|
|
| 57 |
raise HTTPException(status_code=500, detail=str(e))
|
| 58 |
|
| 59 |
@app.get("/")
|
| 60 |
async def root():
|
| 61 |
-
return {"message": "Welcome to the LLM Chat API. Use POST /chat endpoint to get responses."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
+
import logging
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
+
# Set up logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
# Initialize FastAPI app
|
| 14 |
app = FastAPI(
|
| 15 |
title="LLM Chat API",
|
|
|
|
| 22 |
|
| 23 |
class ChatResponse(BaseModel):
|
| 24 |
response: str
|
| 25 |
+
status: str
|
| 26 |
|
| 27 |
def llm_chat_response(text: str) -> str:
|
| 28 |
try:
|
| 29 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 30 |
+
logger.info("Checking HF_TOKEN...")
|
| 31 |
if not HF_TOKEN:
|
| 32 |
+
logger.error("HF_TOKEN not found in environment variables")
|
| 33 |
raise HTTPException(status_code=500, detail="HF_TOKEN not configured")
|
| 34 |
|
| 35 |
+
logger.info("Initializing InferenceClient...")
|
| 36 |
client = InferenceClient(api_key=HF_TOKEN)
|
| 37 |
+
|
| 38 |
messages = [
|
| 39 |
{
|
| 40 |
"role": "user",
|
| 41 |
"content": [
|
| 42 |
{
|
| 43 |
"type": "text",
|
| 44 |
+
"text": text + str(' describe in one line only')
|
| 45 |
}
|
| 46 |
]
|
| 47 |
}
|
| 48 |
]
|
| 49 |
|
| 50 |
+
logger.info("Sending request to model...")
|
| 51 |
response_from_llama = client.chat.completions.create(
|
| 52 |
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
| 53 |
messages=messages,
|
|
|
|
| 55 |
)
|
| 56 |
return response_from_llama.choices[0].message['content']
|
| 57 |
except Exception as e:
|
| 58 |
+
logger.error(f"Error in llm_chat_response: {str(e)}")
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
|
| 61 |
@app.post("/chat", response_model=ChatResponse)
|
| 62 |
async def chat(request: ChatRequest):
|
| 63 |
try:
|
| 64 |
+
logger.info(f"Received chat request with text: {request.text}")
|
| 65 |
response = llm_chat_response(request.text)
|
| 66 |
+
return ChatResponse(response=response, status="success")
|
| 67 |
except HTTPException as he:
|
| 68 |
+
logger.error(f"HTTP Exception in chat endpoint: {str(he)}")
|
| 69 |
raise he
|
| 70 |
except Exception as e:
|
| 71 |
+
logger.error(f"Unexpected error in chat endpoint: {str(e)}")
|
| 72 |
raise HTTPException(status_code=500, detail=str(e))
|
| 73 |
|
| 74 |
@app.get("/")
|
| 75 |
async def root():
|
| 76 |
+
return {"message": "Welcome to the LLM Chat API. Use POST /chat endpoint to get responses."}
|
| 77 |
+
|
| 78 |
+
# Add error handling for 404 and 405 errors
|
| 79 |
+
@app.exception_handler(404)
|
| 80 |
+
async def not_found_handler(request, exc):
|
| 81 |
+
return {"error": "Endpoint not found. Please use POST /chat for queries."}, 404
|
| 82 |
+
|
| 83 |
+
@app.exception_handler(405)
|
| 84 |
+
async def method_not_allowed_handler(request, exc):
|
| 85 |
+
return {"error": "Method not allowed. Please check the API documentation."}, 405
|