Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from openpyxl.drawing.image import Image
|
|
| 7 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
import io
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
@@ -33,10 +35,14 @@ def extract_keywords(product_names):
|
|
| 33 |
|
| 34 |
# Create the filename with the current date and time
|
| 35 |
filename = f'소싱부스트_키워드분석기_{formatted_date}.xlsx'
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Load the workbook and edit the cells
|
| 39 |
-
wb = load_workbook(
|
| 40 |
ws = wb.active
|
| 41 |
|
| 42 |
# Insert the image
|
|
@@ -51,8 +57,8 @@ def extract_keywords(product_names):
|
|
| 51 |
ws['D2'].hyperlink = "https://www.ssboost.co.kr"
|
| 52 |
ws['D2'].style = "Hyperlink"
|
| 53 |
|
| 54 |
-
wb.save(
|
| 55 |
-
return
|
| 56 |
|
| 57 |
@app.post("/extract_keywords_from_file/")
|
| 58 |
async def extract_keywords_from_file(file: UploadFile = File(...)):
|
|
@@ -62,18 +68,20 @@ async def extract_keywords_from_file(file: UploadFile = File(...)):
|
|
| 62 |
if df.empty:
|
| 63 |
raise HTTPException(status_code=400, detail="No data found in the specified range.")
|
| 64 |
unique_product_names = df.iloc[:, 0].dropna().astype(str).unique().tolist()
|
| 65 |
-
|
| 66 |
-
return FileResponse(
|
| 67 |
except Exception as e:
|
| 68 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
| 69 |
|
| 70 |
@app.post("/extract_keywords_from_text/")
|
| 71 |
async def extract_keywords_from_text(text: str):
|
| 72 |
if not text.strip():
|
| 73 |
raise HTTPException(status_code=400, detail="No text provided.")
|
| 74 |
product_names = text.split('\n')
|
| 75 |
-
|
| 76 |
-
return FileResponse(
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
import uvicorn
|
|
|
|
| 7 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
import io
|
| 10 |
+
import tempfile
|
| 11 |
+
import shutil
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 35 |
|
| 36 |
# Create the filename with the current date and time
|
| 37 |
filename = f'소싱부스트_키워드분석기_{formatted_date}.xlsx'
|
| 38 |
+
|
| 39 |
+
# Save the DataFrame to a temporary file
|
| 40 |
+
temp_dir = tempfile.mkdtemp()
|
| 41 |
+
file_path = f"{temp_dir}/{filename}"
|
| 42 |
+
df.to_excel(file_path, index=False, startrow=3) # Save the DataFrame starting from A4
|
| 43 |
|
| 44 |
# Load the workbook and edit the cells
|
| 45 |
+
wb = load_workbook(file_path)
|
| 46 |
ws = wb.active
|
| 47 |
|
| 48 |
# Insert the image
|
|
|
|
| 57 |
ws['D2'].hyperlink = "https://www.ssboost.co.kr"
|
| 58 |
ws['D2'].style = "Hyperlink"
|
| 59 |
|
| 60 |
+
wb.save(file_path)
|
| 61 |
+
return file_path
|
| 62 |
|
| 63 |
@app.post("/extract_keywords_from_file/")
|
| 64 |
async def extract_keywords_from_file(file: UploadFile = File(...)):
|
|
|
|
| 68 |
if df.empty:
|
| 69 |
raise HTTPException(status_code=400, detail="No data found in the specified range.")
|
| 70 |
unique_product_names = df.iloc[:, 0].dropna().astype(str).unique().tolist()
|
| 71 |
+
file_path = extract_keywords(unique_product_names)
|
| 72 |
+
return FileResponse(file_path, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename=file_path.split("/")[-1])
|
| 73 |
except Exception as e:
|
| 74 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 75 |
+
finally:
|
| 76 |
+
shutil.rmtree(temp_dir)
|
| 77 |
|
| 78 |
@app.post("/extract_keywords_from_text/")
|
| 79 |
async def extract_keywords_from_text(text: str):
|
| 80 |
if not text.strip():
|
| 81 |
raise HTTPException(status_code=400, detail="No text provided.")
|
| 82 |
product_names = text.split('\n')
|
| 83 |
+
file_path = extract_keywords(product_names)
|
| 84 |
+
return FileResponse(file_path, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename=file_path.split("/")[-1])
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
import uvicorn
|