Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,27 @@ import requests
|
|
| 3 |
import base64
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Optional
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
# NVIDIA API endpoint and API key
|
| 10 |
NVIDIA_API_URL = "https://ai.api.nvidia.com/v1/gr/meta/llama-3.2-90b-vision-instruct/chat/completions"
|
| 11 |
-
API_KEY = "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" # Replace
|
| 12 |
|
| 13 |
-
# Request model for
|
| 14 |
class TextRequest(BaseModel):
|
| 15 |
message: str
|
| 16 |
max_tokens: Optional[int] = 512
|
| 17 |
temperature: Optional[float] = 1.0
|
| 18 |
top_p: Optional[float] = 1.0
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Function to call the NVIDIA API
|
| 21 |
def call_nvidia_api(payload: dict):
|
| 22 |
headers = {
|
|
@@ -28,12 +35,14 @@ def call_nvidia_api(payload: dict):
|
|
| 28 |
raise HTTPException(status_code=response.status_code, detail="NVIDIA API request failed")
|
| 29 |
return response.json()
|
| 30 |
|
| 31 |
-
#
|
| 32 |
@app.post("/chat/text")
|
| 33 |
async def chat_with_text(request: TextRequest):
|
|
|
|
|
|
|
| 34 |
payload = {
|
| 35 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 36 |
-
"messages":
|
| 37 |
"max_tokens": request.max_tokens,
|
| 38 |
"temperature": request.temperature,
|
| 39 |
"top_p": request.top_p,
|
|
@@ -45,12 +54,9 @@ async def chat_with_text(request: TextRequest):
|
|
| 45 |
except Exception as e:
|
| 46 |
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
@app.post("/chat/vision")
|
| 51 |
async def chat_from_text_with_image_url(request: TextRequest):
|
| 52 |
-
import re
|
| 53 |
-
|
| 54 |
# Detect image URL
|
| 55 |
match = re.search(r'https?://\S+\.(jpg|jpeg|png|webp|gif)', request.message)
|
| 56 |
if not match:
|
|
@@ -65,12 +71,14 @@ async def chat_from_text_with_image_url(request: TextRequest):
|
|
| 65 |
except Exception as e:
|
| 66 |
raise HTTPException(status_code=400, detail=f"Failed to fetch image: {e}")
|
| 67 |
|
| 68 |
-
# Replace image URL
|
| 69 |
modified_message = request.message.replace(image_url, img_tag)
|
| 70 |
|
|
|
|
|
|
|
| 71 |
payload = {
|
| 72 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 73 |
-
"messages":
|
| 74 |
"max_tokens": request.max_tokens,
|
| 75 |
"temperature": request.temperature,
|
| 76 |
"top_p": request.top_p,
|
|
@@ -83,9 +91,14 @@ async def chat_from_text_with_image_url(request: TextRequest):
|
|
| 83 |
except Exception as e:
|
| 84 |
raise HTTPException(status_code=500, detail=str(e))
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
# Root endpoint
|
| 89 |
@app.get("/")
|
| 90 |
async def root():
|
| 91 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import base64
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Optional
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
# NVIDIA API endpoint and API key
|
| 11 |
NVIDIA_API_URL = "https://ai.api.nvidia.com/v1/gr/meta/llama-3.2-90b-vision-instruct/chat/completions"
|
| 12 |
+
API_KEY = "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" # Replace securely in production
|
| 13 |
|
| 14 |
+
# Request model for single user message
|
| 15 |
class TextRequest(BaseModel):
|
| 16 |
message: str
|
| 17 |
max_tokens: Optional[int] = 512
|
| 18 |
temperature: Optional[float] = 1.0
|
| 19 |
top_p: Optional[float] = 1.0
|
| 20 |
|
| 21 |
+
# Common pre-prompts
|
| 22 |
+
PRE_PROMPT_MESSAGES = [
|
| 23 |
+
{"role": "system", "content": "You are a helpful multimodal assistant powered by LLaMA 3.2 Vision-Instruct."},
|
| 24 |
+
{"role": "assistant", "content": "Hi! You can send text or image-based questions. What would you like to know?"}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
# Function to call the NVIDIA API
|
| 28 |
def call_nvidia_api(payload: dict):
|
| 29 |
headers = {
|
|
|
|
| 35 |
raise HTTPException(status_code=response.status_code, detail="NVIDIA API request failed")
|
| 36 |
return response.json()
|
| 37 |
|
| 38 |
+
# /chat/text endpoint: Adds new user message to pre-prompted context
|
| 39 |
@app.post("/chat/text")
|
| 40 |
async def chat_with_text(request: TextRequest):
|
| 41 |
+
messages = PRE_PROMPT_MESSAGES + [{"role": "user", "content": request.message}]
|
| 42 |
+
|
| 43 |
payload = {
|
| 44 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 45 |
+
"messages": messages,
|
| 46 |
"max_tokens": request.max_tokens,
|
| 47 |
"temperature": request.temperature,
|
| 48 |
"top_p": request.top_p,
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
raise HTTPException(status_code=500, detail=str(e))
|
| 56 |
|
| 57 |
+
# /chat/vision endpoint: Handles messages containing image URLs
|
|
|
|
| 58 |
@app.post("/chat/vision")
|
| 59 |
async def chat_from_text_with_image_url(request: TextRequest):
|
|
|
|
|
|
|
| 60 |
# Detect image URL
|
| 61 |
match = re.search(r'https?://\S+\.(jpg|jpeg|png|webp|gif)', request.message)
|
| 62 |
if not match:
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
raise HTTPException(status_code=400, detail=f"Failed to fetch image: {e}")
|
| 73 |
|
| 74 |
+
# Replace image URL in message
|
| 75 |
modified_message = request.message.replace(image_url, img_tag)
|
| 76 |
|
| 77 |
+
messages = PRE_PROMPT_MESSAGES + [{"role": "user", "content": modified_message}]
|
| 78 |
+
|
| 79 |
payload = {
|
| 80 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 81 |
+
"messages": messages,
|
| 82 |
"max_tokens": request.max_tokens,
|
| 83 |
"temperature": request.temperature,
|
| 84 |
"top_p": request.top_p,
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
raise HTTPException(status_code=500, detail=str(e))
|
| 93 |
|
|
|
|
|
|
|
| 94 |
# Root endpoint
|
| 95 |
@app.get("/")
|
| 96 |
async def root():
|
| 97 |
+
return {
|
| 98 |
+
"message": "Welcome to the NVIDIA Vision Chat API!",
|
| 99 |
+
"endpoints": {
|
| 100 |
+
"/chat/text": "Send plain text questions (just provide your message).",
|
| 101 |
+
"/chat/vision": "Send a message with an image URL (e.g. 'What is this? https://example.com/cat.jpg')",
|
| 102 |
+
},
|
| 103 |
+
"note": "You do NOT need to include assistant history or system roles — it's pre-injected automatically."
|
| 104 |
+
}
|