Update app.py
Browse files
app.py
CHANGED
|
@@ -60,25 +60,28 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_id):
|
|
| 60 |
async def upload_file(LLM_URL, LLM_API, file_path, user_id):
|
| 61 |
if not os.path.exists(file_path):
|
| 62 |
return f"Error: File {file_path} not found"
|
|
|
|
|
|
|
| 63 |
mime_type, _ = mimetypes.guess_type(file_path)
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
print("Uploading file:", filename, "mime_type:", mime_type)
|
| 67 |
-
|
| 68 |
with open(file_path, 'rb') as f:
|
| 69 |
async with aiohttp.ClientSession() as session:
|
| 70 |
form_data = aiohttp.FormData()
|
| 71 |
-
form_data.add_field('file', f, filename=
|
| 72 |
form_data.add_field('user', user_id)
|
| 73 |
async with session.post(
|
| 74 |
-
f"{LLM_URL}/files/upload",
|
| 75 |
headers={"Authorization": f"Bearer {LLM_API}"},
|
| 76 |
data=form_data
|
| 77 |
) as response:
|
| 78 |
if response.status == 404:
|
| 79 |
return "Error: Endpoint not found (404)"
|
| 80 |
response_text = await response.text()
|
| 81 |
-
print("Upload response:", response_text)
|
| 82 |
try:
|
| 83 |
return json.loads(response_text)
|
| 84 |
except json.JSONDecodeError:
|
|
|
|
| 60 |
async def upload_file(LLM_URL, LLM_API, file_path, user_id):
|
| 61 |
if not os.path.exists(file_path):
|
| 62 |
return f"Error: File {file_path} not found"
|
| 63 |
+
|
| 64 |
+
# ✅ 先定義 filename 和 mime_type
|
| 65 |
mime_type, _ = mimetypes.guess_type(file_path)
|
| 66 |
+
filename = os.path.basename(file_path)
|
| 67 |
+
|
| 68 |
+
# ✅ print 放在這裡
|
| 69 |
print("Uploading file:", filename, "mime_type:", mime_type)
|
| 70 |
+
|
| 71 |
with open(file_path, 'rb') as f:
|
| 72 |
async with aiohttp.ClientSession() as session:
|
| 73 |
form_data = aiohttp.FormData()
|
| 74 |
+
form_data.add_field('file', f, filename=filename, content_type=mime_type)
|
| 75 |
form_data.add_field('user', user_id)
|
| 76 |
async with session.post(
|
| 77 |
+
f"{LLM_URL}/files/upload",
|
| 78 |
headers={"Authorization": f"Bearer {LLM_API}"},
|
| 79 |
data=form_data
|
| 80 |
) as response:
|
| 81 |
if response.status == 404:
|
| 82 |
return "Error: Endpoint not found (404)"
|
| 83 |
response_text = await response.text()
|
| 84 |
+
print("Upload response:", response_text)
|
| 85 |
try:
|
| 86 |
return json.loads(response_text)
|
| 87 |
except json.JSONDecodeError:
|