File size: 4,289 Bytes
c8f6495 e6a90e9 c8f6495 e6a90e9 c8f6495 e6a90e9 5cf48c0 e6a90e9 67a7d26 e6a90e9 5cf48c0 e6a90e9 c8f6495 e6a90e9 5cf48c0 e6a90e9 5cf48c0 e6a90e9 5cf48c0 e6a90e9 5cf48c0 e6a90e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
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]:
# Parse JSON string form field into dict
try:
state_data = json.loads(state)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field")
# Save the uploaded file asynchronously
dest = MEDIA_DIR / file.filename
data = await file.read()
dest.write_bytes(data)
# Initialize agent
assistant = Assistant(state=state_data)
await assistant.authorization()
# Preprocess file
transcription = await preprocess_audio(str(dest))
state_data["message"] = HumanMessage(
content=transcription
)
# Call the agents
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]:
# Parse JSON string form field into dict
try:
state_data = json.loads(state)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field")
# Save the uploaded file asynchronously
dest = MEDIA_DIR / file.filename
data = await file.read()
dest.write_bytes(data)
# Initialize agent
assistant = Assistant(state=state_data)
await assistant.authorization()
# Preprocess file
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}"
)
# Call the agents
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]:
# Parse JSON string form field into dict
try:
state_data = json.loads(state)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field")
# Save the uploaded file asynchronously
dest = MEDIA_DIR / file.filename
data = await file.read()
dest.write_bytes(data)
#Initialize agent
assistant = Assistant(state=state_data)
await assistant.authorization()
# Preprocess file
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}"
)
# Call the agents
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]:
# Parse JSON string form field into dict
try:
state_data = json.loads(state)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field")
# Reformat the message
state_data['message'] = HumanMessage(content=state_data['message']['content'])
# Call the agents
assistant = Assistant(state=state_data)
await assistant.authorization()
response = await assistant.run()
return response
@app.post("/authorization")
async def authorization_mode(
state: str = Form(...),
):
# Parse JSON string form field into dict
try:
state_data = json.loads(state)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'state' form field")
# Call the agents
assistant = Assistant(state=state_data)
await assistant.authorization()
return {"message": "Authorization successful"}
|