Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,50 @@
|
|
|
|
|
|
1 |
import os
|
2 |
-
from
|
3 |
-
from
|
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 |
-
return
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
2 |
+
from transformers import pipeline
|
3 |
import os
|
4 |
+
from typing import List
|
5 |
+
from pydantic import BaseModel
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Initialize the AI model
|
10 |
+
model_name = "gpt-3.5-turbo"
|
11 |
+
generator = pipeline('text-generation', model=model_name)
|
12 |
+
|
13 |
+
class FileUpdate(BaseModel):
|
14 |
+
filename: str
|
15 |
+
content: str
|
16 |
+
|
17 |
+
@app.post("/generate")
|
18 |
+
def generate_text(prompt: str):
|
19 |
+
try:
|
20 |
+
result = generator(prompt, max_length=100)
|
21 |
+
return {"response": result[0]['generated_text']}
|
22 |
+
except Exception as e:
|
23 |
+
raise HTTPException(status_code=500, detail=str(e))
|
24 |
+
|
25 |
+
@app.post("/upload")
|
26 |
+
async def upload_file(file: UploadFile = File(...)):
|
27 |
+
try:
|
28 |
+
with open(f"./files/{file.filename}", "wb") as f:
|
29 |
+
content = await file.read()
|
30 |
+
f.write(content)
|
31 |
+
return {"filename": file.filename}
|
32 |
+
except Exception as e:
|
33 |
+
raise HTTPException(status_code=500, detail=str(e))
|
34 |
+
|
35 |
+
@app.post("/update")
|
36 |
+
def update_file(file_update: FileUpdate):
|
37 |
+
try:
|
38 |
+
with open(f"./files/{file_update.filename}", "w") as f:
|
39 |
+
f.write(file_update.content)
|
40 |
+
return {"status": "success"}
|
41 |
+
except Exception as e:
|
42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
43 |
+
|
44 |
+
@app.get("/files")
|
45 |
+
def list_files():
|
46 |
+
try:
|
47 |
+
files = os.listdir("./files")
|
48 |
+
return {"files": files}
|
49 |
+
except Exception as e:
|
50 |
+
raise HTTPException(status_code=500, detail=str(e))
|