Oluwatoni commited on
Commit
623cc41
·
verified ·
1 Parent(s): 8836a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -1,27 +1,26 @@
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from llmlingua import PromptCompressor
4
- import os
5
 
6
  app = FastAPI()
7
 
8
- # Initialize LLM Lingua with CPU
9
- compressor = PromptCompressor( device_map="auto") # or set device_map=None
10
 
11
  class TextInput(BaseModel):
12
  text: str
13
 
14
- class CompressedOutput(BaseModel):
15
  compressed_text: str
16
 
17
- @app.post("/compress", response_model=CompressedOutput)
18
  async def compress_text(input: TextInput):
19
  try:
20
- compressed = compressor.compress_prompt(input.text)
21
- return CompressedOutput(compressed_text=compressed)
22
  except Exception as e:
23
  raise HTTPException(status_code=500, detail=str(e))
24
 
25
- if __name__ == "__main__":
26
- import uvicorn
27
- uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
 
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from llmlingua import PromptCompressor
 
4
 
5
  app = FastAPI()
6
 
7
+ # Initialize LLMLingua
8
+ compressor = PromptCompressor(model_name="openai-community/gpt2", device_map="cpu")
9
 
10
  class TextInput(BaseModel):
11
  text: str
12
 
13
+ class TextOutput(BaseModel):
14
  compressed_text: str
15
 
16
+ @app.post("/compress", response_model=TextOutput)
17
  async def compress_text(input: TextInput):
18
  try:
19
+ compressed_text = compressor.compress_prompt(input.text)
20
+ return TextOutput(compressed_text=compressed_text)
21
  except Exception as e:
22
  raise HTTPException(status_code=500, detail=str(e))
23
 
24
+ @app.get("/")
25
+ async def root():
26
+ return {"message": "Welcome to the LLMLingua Text Compression API"}