Spaces:
Sleeping
Sleeping
chabane
commited on
Commit
·
74ace95
1
Parent(s):
4e993f8
add snapshot to load the models
Browse files- .gitignore +1 -0
- main.py +58 -70
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
models/
|
main.py
CHANGED
|
@@ -9,20 +9,59 @@ import io
|
|
| 9 |
import base64
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
import torch
|
| 12 |
-
import tensorflow
|
| 13 |
-
|
| 14 |
-
from transformers import BartForConditionalGeneration, BartTokenizer
|
| 15 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 16 |
import fitz
|
| 17 |
from docx import Document
|
| 18 |
from pptx import Presentation
|
| 19 |
import seaborn as sns
|
| 20 |
import PIL.Image as Image
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
app=FastAPI()
|
|
@@ -34,65 +73,19 @@ app.add_middleware(
|
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
| 37 |
-
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
| 38 |
-
try:
|
| 39 |
-
interpreter =None
|
| 40 |
-
print("installing interpreter ...")
|
| 41 |
-
interpreter =pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 42 |
-
if interpreter is None :
|
| 43 |
-
print("\n\n interpreter is nonne \n\n")
|
| 44 |
-
else:
|
| 45 |
-
print(" interpreter installed.")
|
| 46 |
-
#interpreter_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 47 |
-
#interpreter_processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 48 |
-
#interpreter_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 49 |
-
except Exception as exp:
|
| 50 |
-
print("[ERROR] Can't load nlpconnect/vit-gpt2-image-captioning")
|
| 51 |
-
print(str(exp))
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
-
try:
|
| 56 |
-
summarizer=None
|
| 57 |
-
print ("installing summarizer ...")
|
| 58 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 59 |
-
if summarizer is None :
|
| 60 |
-
print("\n\n summarizer is nonne \n\n")
|
| 61 |
-
else:
|
| 62 |
-
print(" summarizer installed.")
|
| 63 |
-
except Exception as exp:
|
| 64 |
-
print("[ERROR] Can't load facebook/bart-large-cnn ")
|
| 65 |
-
print(str(exp))
|
| 66 |
|
| 67 |
-
#try:
|
| 68 |
-
# summarizer_model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
| 69 |
-
#except OSError as e:
|
| 70 |
-
# print(f"[INFO] PyTorch weights not found. Falling back to TensorFlow weights.\n{e}")
|
| 71 |
-
# summarizer_model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn", from_tf=True)
|
| 72 |
|
| 73 |
-
#summarizer_tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
-
try:
|
| 78 |
-
generator=None
|
| 79 |
-
print("installing generator ...")
|
| 80 |
-
generator = pipeline("text-generation", model="deepseek-ai/deepseek-coder-1.3b-instruct")
|
| 81 |
-
if generator is None :
|
| 82 |
-
print("\n\n generator is nonne \n\n")
|
| 83 |
-
else:
|
| 84 |
-
print(" generator installed.")
|
| 85 |
-
except Exception as exp:
|
| 86 |
-
print("[ERROR] Can't load deepseek-ai/deepseek-coder-1.3b-instruct ")
|
| 87 |
-
print(str(exp))
|
| 88 |
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#except Exception as exp :
|
| 94 |
-
# print("[ERROR] Can't load deepseek-ai/deepseek-coder-1.3b-instruct ")
|
| 95 |
-
# print(str(exp))
|
| 96 |
|
| 97 |
|
| 98 |
app.mount("/static",StaticFiles(directory='static'),'static')
|
|
@@ -112,14 +105,6 @@ def index(req:Request):
|
|
| 112 |
return templates.TemplateResponse('ImageInterpretation.html',{'request':req})
|
| 113 |
|
| 114 |
|
| 115 |
-
@app.post('/get')
|
| 116 |
-
def g(f:str):
|
| 117 |
-
global generator
|
| 118 |
-
return generator(f)[0]["generated_text"]
|
| 119 |
-
@app.post('/gets')
|
| 120 |
-
def g(f:str):
|
| 121 |
-
global summarizer
|
| 122 |
-
return summarizer(f)[0]['summary_text']
|
| 123 |
|
| 124 |
|
| 125 |
|
|
@@ -130,12 +115,10 @@ def caption(file:UploadFile=File(...)):
|
|
| 130 |
if extension not in Supported_extensions:
|
| 131 |
return {"error": "Unsupported file type"}
|
| 132 |
image = Image.open(file.file)
|
| 133 |
-
|
|
|
|
| 134 |
caption = interpreter(image)
|
| 135 |
-
|
| 136 |
-
#output_ids = interpreter_model.generate(pixel_values, max_length=16, num_beams=4)
|
| 137 |
-
#caption = interpreter_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 138 |
-
#return {"caption":caption}
|
| 139 |
return {"caption": caption[0]['generated_text']}
|
| 140 |
|
| 141 |
@app.post("/summerize")
|
|
@@ -154,8 +137,13 @@ def summerzation(file:UploadFile=File(...)):
|
|
| 154 |
return {"error": "Unsupported file type"}
|
| 155 |
|
| 156 |
result=""
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
return {"summary": result}
|
| 160 |
|
| 161 |
|
|
@@ -193,7 +181,7 @@ error.
|
|
| 193 |
|
| 194 |
##Prompt: {prompt}.
|
| 195 |
"""
|
| 196 |
-
|
| 197 |
output = generator(message, max_length=1000)
|
| 198 |
match = re.search(r'```python(.*?)```', output[0]["generated_text"], re.DOTALL)
|
| 199 |
code =''
|
|
|
|
| 9 |
import base64
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
import torch
|
| 12 |
+
import tensorflow as tf
|
| 13 |
+
|
|
|
|
|
|
|
| 14 |
import fitz
|
| 15 |
from docx import Document
|
| 16 |
from pptx import Presentation
|
| 17 |
import seaborn as sns
|
| 18 |
import PIL.Image as Image
|
| 19 |
+
import fitz
|
| 20 |
|
| 21 |
+
from huggingface_hub import snapshot_download
|
| 22 |
+
from transformers import (
|
| 23 |
+
TFAutoModelForVision2Seq, AutoProcessor,
|
| 24 |
+
AutoTokenizer, AutoModelForSeq2SeqLM,
|
| 25 |
+
AutoModelForCausalLM,pipeline
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# === 1. Load BLIP Image Captioning (TensorFlow) ===
|
| 29 |
+
try:
|
| 30 |
+
print("[Info] installing Salesforce/blip-image-captioning-base ....")
|
| 31 |
+
blip_dir = "./models/blip-base-tf"
|
| 32 |
+
snapshot_download("Salesforce/blip-image-captioning-base", local_dir=blip_dir, local_dir_use_symlinks=False)
|
| 33 |
+
interpreter = pipeline("image-captioning", model="Salesforce/blip-image-captioning-base")
|
| 34 |
+
print("[Info] Salesforce/blip-image-captioning-base is inatalled.")
|
| 35 |
+
except Exception as exp:
|
| 36 |
+
print("Can't load the model Salesforce/blip-image-captioning-base")
|
| 37 |
+
print(f"[Error] {str(exp)}")
|
| 38 |
|
| 39 |
+
# === 2. Load BART Summarization (PyTorch) ===
|
| 40 |
+
try:
|
| 41 |
+
print("[Info] installing facebook/bart-large-cnn ....")
|
| 42 |
+
bart_dir = "./models/bart-large-cnn"
|
| 43 |
+
snapshot_download("facebook/bart-large-cnn", local_dir=bart_dir, local_dir_use_symlinks=False)
|
| 44 |
+
bart_tokenizer = AutoTokenizer.from_pretrained(bart_dir)
|
| 45 |
+
bart_model = AutoModelForSeq2SeqLM.from_pretrained(bart_dir)
|
| 46 |
+
summarizer = pipeline("summarization", model=bart_model, tokenizer=bart_tokenizer)
|
| 47 |
+
print("[Info] facebook/bart-large-cnn is installed")
|
| 48 |
+
except Exception as exp:
|
| 49 |
+
print("Can't load the model facebook/bart-large-cnn")
|
| 50 |
+
print(f"[Error] {str(exp)}")
|
| 51 |
|
| 52 |
+
# === 3. Load DeepSeek Coder (PyTorch with trust_remote_code) ===
|
| 53 |
+
try:
|
| 54 |
+
print("[Info] installing deepseek-ai/deepseek-coder-1.3b-instruct ")
|
| 55 |
+
deepseek_dir = "./models/deepseek-coder"
|
| 56 |
+
snapshot_download("deepseek-ai/deepseek-coder-1.3b-instruct", local_dir=deepseek_dir, local_dir_use_symlinks=False)
|
| 57 |
+
deepseek_tokenizer = AutoTokenizer.from_pretrained(deepseek_dir, trust_remote_code=True)
|
| 58 |
+
deepseek_model = AutoModelForCausalLM.from_pretrained(deepseek_dir, trust_remote_code=True)
|
| 59 |
+
generator = pipeline("text-generation", model=deepseek_model, tokenizer=deepseek_tokenizer)
|
| 60 |
+
|
| 61 |
+
print("[Info] facebook/bart-large-cnn is installed")
|
| 62 |
+
except Exception as exp:
|
| 63 |
+
print("Can't load the model deepseek-ai/deepseek-coder-1.3b-instruct")
|
| 64 |
+
print(f"[Error] {str(exp)}")
|
| 65 |
|
| 66 |
|
| 67 |
app=FastAPI()
|
|
|
|
| 73 |
allow_headers=["*"],
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
app.mount("/static",StaticFiles(directory='static'),'static')
|
|
|
|
| 105 |
return templates.TemplateResponse('ImageInterpretation.html',{'request':req})
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
|
|
|
|
| 115 |
if extension not in Supported_extensions:
|
| 116 |
return {"error": "Unsupported file type"}
|
| 117 |
image = Image.open(file.file)
|
| 118 |
+
global interpreter
|
| 119 |
+
|
| 120 |
caption = interpreter(image)
|
| 121 |
+
|
|
|
|
|
|
|
|
|
|
| 122 |
return {"caption": caption[0]['generated_text']}
|
| 123 |
|
| 124 |
@app.post("/summerize")
|
|
|
|
| 137 |
return {"error": "Unsupported file type"}
|
| 138 |
|
| 139 |
result=""
|
| 140 |
+
global summarizer
|
| 141 |
+
for i in range(0, len(text), 1024):
|
| 142 |
+
try:
|
| 143 |
+
summary = summarizer(text[i:i+1024], max_length=150, min_length=30, do_sample=False)
|
| 144 |
+
result += summary[0]['summary_text']
|
| 145 |
+
except Exception as e:
|
| 146 |
+
return {"error": f"Summarization failed: {str(e)}"}
|
| 147 |
return {"summary": result}
|
| 148 |
|
| 149 |
|
|
|
|
| 181 |
|
| 182 |
##Prompt: {prompt}.
|
| 183 |
"""
|
| 184 |
+
global generator
|
| 185 |
output = generator(message, max_length=1000)
|
| 186 |
match = re.search(r'```python(.*?)```', output[0]["generated_text"], re.DOTALL)
|
| 187 |
code =''
|