Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
9 |
-
compressor = PromptCompressor( device_map="
|
10 |
|
11 |
class TextInput(BaseModel):
|
12 |
text: str
|
13 |
|
14 |
-
class
|
15 |
compressed_text: str
|
16 |
|
17 |
-
@app.post("/compress", response_model=
|
18 |
async def compress_text(input: TextInput):
|
19 |
try:
|
20 |
-
|
21 |
-
return
|
22 |
except Exception as e:
|
23 |
raise HTTPException(status_code=500, detail=str(e))
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
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"}
|