llm_lingua / app.py
Oluwatoni's picture
Update app.py
8836a04 verified
raw
history blame
794 Bytes
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from llmlingua import PromptCompressor
import os
app = FastAPI()
# Initialize LLM Lingua with CPU
compressor = PromptCompressor( device_map="auto") # or set device_map=None
class TextInput(BaseModel):
text: str
class CompressedOutput(BaseModel):
compressed_text: str
@app.post("/compress", response_model=CompressedOutput)
async def compress_text(input: TextInput):
try:
compressed = compressor.compress_prompt(input.text)
return CompressedOutput(compressed_text=compressed)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))