Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,28 +47,33 @@ async def chat_with_text(request: TextRequest):
|
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
-
@app.post("/chat/
|
| 51 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
-
img_response = requests.get(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
base64_image = base64.b64encode(image_data).decode("utf-8")
|
| 58 |
except Exception as e:
|
| 59 |
-
raise HTTPException(status_code=400, detail=f"
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
payload = {
|
| 62 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 63 |
-
"messages": [
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
}
|
| 68 |
-
],
|
| 69 |
-
"max_tokens": 512,
|
| 70 |
-
"temperature": 1.0,
|
| 71 |
-
"top_p": 1.0,
|
| 72 |
"stream": False,
|
| 73 |
}
|
| 74 |
|
|
@@ -79,6 +84,7 @@ async def chat_with_image_url(request: ImageURLRequest):
|
|
| 79 |
raise HTTPException(status_code=500, detail=str(e))
|
| 80 |
|
| 81 |
|
|
|
|
| 82 |
# Root endpoint
|
| 83 |
@app.get("/")
|
| 84 |
async def root():
|
|
|
|
| 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:
|
| 57 |
+
raise HTTPException(status_code=400, detail="No image URL found")
|
| 58 |
+
|
| 59 |
+
image_url = match.group(0)
|
| 60 |
try:
|
| 61 |
+
img_response = requests.get(image_url)
|
| 62 |
+
img_response.raise_for_status()
|
| 63 |
+
base64_image = base64.b64encode(img_response.content).decode("utf-8")
|
| 64 |
+
img_tag = f'<img src="data:image/png;base64,{base64_image}" />'
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
+
raise HTTPException(status_code=400, detail=f"Failed to fetch image: {e}")
|
| 67 |
+
|
| 68 |
+
# Replace image URL with the base64 image
|
| 69 |
+
modified_message = request.message.replace(image_url, img_tag)
|
| 70 |
|
| 71 |
payload = {
|
| 72 |
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 73 |
+
"messages": [{"role": "user", "content": modified_message}],
|
| 74 |
+
"max_tokens": request.max_tokens,
|
| 75 |
+
"temperature": request.temperature,
|
| 76 |
+
"top_p": request.top_p,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"stream": False,
|
| 78 |
}
|
| 79 |
|
|
|
|
| 84 |
raise HTTPException(status_code=500, detail=str(e))
|
| 85 |
|
| 86 |
|
| 87 |
+
|
| 88 |
# Root endpoint
|
| 89 |
@app.get("/")
|
| 90 |
async def root():
|