Soltane777 commited on
Commit
cce29c9
·
verified ·
1 Parent(s): 8819f57

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -36
main.py CHANGED
@@ -1,36 +1,33 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- from fastapi.staticfiles import StaticFiles
3
- import os
4
-
5
- app = FastAPI()
6
-
7
- # ربط مجلد static للواجهة الأمامية
8
- app.mount("/static", StaticFiles(directory="static"), name="static")
9
-
10
- @app.get("/")
11
- async def root():
12
- return {"message": "Welcome to AI Web App"}
13
-
14
- from transformers import pipeline
15
-
16
- # تحميل نموذج لتلخيص النصوص
17
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
18
- # تحميل نموذج لتفسير الصور (يمكن استبداله بـ Qwen إذا كان متاحًا)
19
- image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
20
-
21
- from tika import parser
22
-
23
- @app.post("/summarize")
24
- async def summarize_document(file: UploadFile = File(...)):
25
- # استخراج النص من المستند
26
- content = parser.from_buffer(await file.read())["content"]
27
- # تلخيص النص
28
- summary = summarizer(content, max_length=130, min_length=30, do_sample=False)
29
- return {"summary": summary[0]["summary_text"]}
30
-
31
- @app.post("/caption")
32
- async def caption_image(file: UploadFile = File(...)):
33
- # تفسير الصورة
34
- caption = image_captioner(await file.read())
35
- return {"caption": caption[0]["generated_text"]}
36
-
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.staticfiles import StaticFiles
3
+ from transformers import pipeline
4
+ from tika import parser
5
+ import os
6
+
7
+ app = FastAPI()
8
+
9
+ # Mount static folder for frontend
10
+ app.mount("/static", StaticFiles(directory="static"), name="static")
11
+
12
+ # Define a writable cache directory inside the container
13
+ CACHE_DIR = "/app/cache"
14
+ os.makedirs(CACHE_DIR, exist_ok=True) # Create the directory if it doesn’t exist
15
+
16
+ # Load models with custom cache directory
17
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn", cache_dir=CACHE_DIR)
18
+ image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", cache_dir=CACHE_DIR)
19
+
20
+ @app.get("/")
21
+ async def root():
22
+ return {"message": "Welcome to AI Web App"}
23
+
24
+ @app.post("/summarize")
25
+ async def summarize_document(file: UploadFile = File(...)):
26
+ content = parser.from_buffer(await file.read())["content"]
27
+ summary = summarizer(content, max_length=130, min_length=30, do_sample=False)
28
+ return {"summary": summary[0]["summary_text"]}
29
+
30
+ @app.post("/caption")
31
+ async def caption_image(file: UploadFile = File(...)):
32
+ caption = image_captioner(await file.read())
33
+ return {"caption": caption[0]["generated_text"]}