|
from agent.file_preprocessing import preprocess_file, preprocess_audio |
|
from fastapi import FastAPI, UploadFile, File, HTTPException |
|
from langchain_core.messages import HumanMessage |
|
from agent.multi_agent import Assistant |
|
from typing import Any, Dict |
|
from fastapi import Form |
|
from pathlib import Path |
|
import asyncio |
|
import json |
|
import os |
|
|
|
MEDIA_DIR = Path("mediafiles") |
|
(MEDIA_DIR.mkdir(exist_ok=True)) |
|
|
|
app = FastAPI() |
|
|
|
@app.post("/voice") |
|
async def voice_mode( |
|
state: str = Form(...), |
|
file: UploadFile = File(...) |
|
) -> Dict[str, Any]: |
|
|
|
try: |
|
state_data = json.loads(state) |
|
except json.JSONDecodeError: |
|
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field") |
|
|
|
|
|
dest = MEDIA_DIR / file.filename |
|
data = await file.read() |
|
dest.write_bytes(data) |
|
|
|
|
|
assistant = Assistant(state=state_data) |
|
await assistant.authorization() |
|
|
|
|
|
transcription = await preprocess_audio(str(dest)) |
|
state_data["message"] = HumanMessage( |
|
content=transcription |
|
) |
|
|
|
assistant.state = state_data |
|
response = await assistant.run() |
|
os.remove(str(dest)) |
|
return response |
|
|
|
@app.post("/image") |
|
async def image_mode( |
|
state: str = Form(...), |
|
file: UploadFile = File(...) |
|
) -> Dict[str, Any]: |
|
|
|
try: |
|
state_data = json.loads(state) |
|
except json.JSONDecodeError: |
|
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field") |
|
|
|
|
|
dest = MEDIA_DIR / file.filename |
|
data = await file.read() |
|
dest.write_bytes(data) |
|
|
|
|
|
assistant = Assistant(state=state_data) |
|
await assistant.authorization() |
|
|
|
|
|
file_contents = await preprocess_file(str(dest)) |
|
state_data["message"] = HumanMessage( |
|
content=f"{state_data.get('message', {}).get('content' 'User uploaded the image.')} \nImage Description: {file_contents}" |
|
) |
|
|
|
|
|
assistant.state = state_data |
|
response = await assistant.run() |
|
os.remove(str(dest)) |
|
return response |
|
|
|
@app.post("/file") |
|
async def file_mode( |
|
state: str = Form(...), |
|
file: UploadFile = File(...) |
|
) -> Dict[str, Any]: |
|
|
|
try: |
|
state_data = json.loads(state) |
|
except json.JSONDecodeError: |
|
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field") |
|
|
|
|
|
dest = MEDIA_DIR / file.filename |
|
data = await file.read() |
|
dest.write_bytes(data) |
|
|
|
|
|
assistant = Assistant(state=state_data) |
|
await assistant.authorization() |
|
|
|
|
|
file_contents = await preprocess_file(str(dest)) |
|
state_data["message"] = HumanMessage( |
|
content=f"{state_data.get('message', {}).get('content' 'User uploaded the file.')} \nFile description: {file_contents}" |
|
) |
|
|
|
|
|
assistant.state = state_data |
|
response = await assistant.run() |
|
os.remove(str(dest)) |
|
return response |
|
|
|
@app.post("/text") |
|
async def text_mode( |
|
state: str = Form(...), |
|
) -> Dict[str, Any]: |
|
|
|
try: |
|
state_data = json.loads(state) |
|
except json.JSONDecodeError: |
|
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field") |
|
|
|
state_data['message'] = HumanMessage(content=state_data['message']['content']) |
|
|
|
assistant = Assistant(state=state_data) |
|
await assistant.authorization() |
|
response = await assistant.run() |
|
return response |
|
|
|
@app.post("/authorization") |
|
async def authorization_mode( |
|
state: str = Form(...), |
|
): |
|
|
|
try: |
|
state_data = json.loads(state) |
|
except json.JSONDecodeError: |
|
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field") |
|
|
|
assistant = Assistant(state=state_data) |
|
await assistant.authorization() |
|
return {"message": "Authorization successful"} |
|
|