from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import HTMLResponse from fastapi.responses import StreamingResponse from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from io import StringIO import os from llmlingua import PromptCompressor llm_lingua = PromptCompressor( model_name="microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank", use_llmlingua2=True, # Whether to use llmlingua-2 device_map="cpu" ) app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class Prompt(BaseModel): original_prompt: str @app.post("/get_compressed_text") async def get_compressed_text(prompt: Prompt): compressed_prompt = llm_lingua.compress_prompt(prompt.original_prompt, instruction="", question="") print("compressed") return compressed_prompt