Spaces:
Sleeping
Sleeping
update app file with cuda
Browse files
app.py
CHANGED
@@ -1,3 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from flask import Flask, request, render_template, jsonify
|
3 |
import re
|
@@ -11,6 +96,9 @@ from nltk.stem import WordNetLemmatizer
|
|
11 |
# Ensure NLTK uses the correct data path
|
12 |
nltk.data.path.append(os.getenv('NLTK_DATA'))
|
13 |
|
|
|
|
|
|
|
14 |
app = Flask(__name__)
|
15 |
|
16 |
# Ensure the Transformers cache directory is set correctly
|
@@ -63,13 +151,14 @@ def summarize():
|
|
63 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
64 |
|
65 |
gen_kwargs = {"length_penalty": 0.8, "num_beams": 8, "max_length": 128}
|
66 |
-
pipe = pipeline("summarization", model=model, tokenizer=tokenizer, device=device)
|
67 |
|
68 |
text = pipe(input_text, **gen_kwargs)[0]["summary_text"]
|
69 |
output_text = replace_pronouns(remove_spaces_before_punctuation(text))
|
70 |
|
71 |
# Clear the GPU cache
|
72 |
-
|
|
|
73 |
|
74 |
# Return the summary
|
75 |
return jsonify({'summary': output_text})
|
@@ -80,3 +169,4 @@ def index():
|
|
80 |
|
81 |
if __name__ == '__main__':
|
82 |
app.run(host='0.0.0.0', debug=True, port=7860)
|
|
|
|
1 |
+
# import os
|
2 |
+
# from flask import Flask, request, render_template, jsonify
|
3 |
+
# import re
|
4 |
+
# import nltk
|
5 |
+
# import torch
|
6 |
+
# from pathlib import Path
|
7 |
+
# from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
+
# from nltk.tokenize import word_tokenize
|
9 |
+
# from nltk.stem import WordNetLemmatizer
|
10 |
+
|
11 |
+
# # Ensure NLTK uses the correct data path
|
12 |
+
# nltk.data.path.append(os.getenv('NLTK_DATA'))
|
13 |
+
|
14 |
+
# app = Flask(__name__)
|
15 |
+
|
16 |
+
# # Ensure the Transformers cache directory is set correctly
|
17 |
+
# os.environ['TRANSFORMERS_CACHE'] = os.getenv('TRANSFORMERS_CACHE')
|
18 |
+
|
19 |
+
# tokenizer = AutoTokenizer.from_pretrained(Path("summary/tokenizer"))
|
20 |
+
# model_name = "summary/pegasus-samsum-model"
|
21 |
+
|
22 |
+
# def remove_spaces_before_punctuation(text):
|
23 |
+
# pattern = re.compile(r'(\s+)([.,;!?])')
|
24 |
+
# result = pattern.sub(r'\2', text)
|
25 |
+
# result = re.sub(r'\[|\]', '', result)
|
26 |
+
# return result
|
27 |
+
|
28 |
+
# def replace_pronouns(text):
|
29 |
+
# # Replace "they" with "he" or "she" based on context
|
30 |
+
# text = re.sub(r'\bthey\b', 'He/She', text, flags=re.IGNORECASE)
|
31 |
+
# text = re.sub(r'\b(are|have|were)\b', lambda x: {'are': 'is', 'have': 'has', 'were': 'was'}[x.group()], text)
|
32 |
+
# return text
|
33 |
+
|
34 |
+
# def clean_and_lemmatize(text):
|
35 |
+
# # Remove digits, symbols, punctuation marks, and newline characters
|
36 |
+
# text = re.sub(r'\d+', '', text)
|
37 |
+
# text = re.sub(r'[^\w\s,-]', '', text.replace('\n', ''))
|
38 |
+
# # Tokenize the text
|
39 |
+
# tokens = word_tokenize(text.lower())
|
40 |
+
# # Initialize lemmatizer
|
41 |
+
# lemmatizer = WordNetLemmatizer()
|
42 |
+
# # Lemmatize each token and join back into a sentence
|
43 |
+
# lemmatized_text = ' '.join([lemmatizer.lemmatize(token) for token in tokens])
|
44 |
+
# return lemmatized_text
|
45 |
+
|
46 |
+
# @app.route('/summarize', methods=['POST'])
|
47 |
+
# def summarize():
|
48 |
+
# # Get the input text from the request
|
49 |
+
# input_text = request.form['input_text']
|
50 |
+
|
51 |
+
# # Tokenize the input text
|
52 |
+
# tokens_org_text = tokenizer.tokenize(input_text)
|
53 |
+
# sequence_length_org_text = len(tokens_org_text)
|
54 |
+
|
55 |
+
# input_text = clean_and_lemmatize(input_text)
|
56 |
+
# tokens = tokenizer.tokenize(input_text)
|
57 |
+
# sequence_length = len(tokens)
|
58 |
+
|
59 |
+
# if sequence_length >= 1024:
|
60 |
+
# return jsonify({'error': 'Input text exceeds maximum token length of 1023.'})
|
61 |
+
|
62 |
+
# # Initialize model variable
|
63 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
64 |
+
|
65 |
+
# gen_kwargs = {"length_penalty": 0.8, "num_beams": 8, "max_length": 128}
|
66 |
+
# pipe = pipeline("summarization", model=model, tokenizer=tokenizer, device=device)
|
67 |
+
|
68 |
+
# text = pipe(input_text, **gen_kwargs)[0]["summary_text"]
|
69 |
+
# output_text = replace_pronouns(remove_spaces_before_punctuation(text))
|
70 |
+
|
71 |
+
# # Clear the GPU cache
|
72 |
+
# torch.cuda.empty_cache()
|
73 |
+
|
74 |
+
# # Return the summary
|
75 |
+
# return jsonify({'summary': output_text})
|
76 |
+
|
77 |
+
# @app.route('/')
|
78 |
+
# def index():
|
79 |
+
# return render_template('index.html')
|
80 |
+
|
81 |
+
# if __name__ == '__main__':
|
82 |
+
# app.run(host='0.0.0.0', debug=True, port=7860)
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
import os
|
87 |
from flask import Flask, request, render_template, jsonify
|
88 |
import re
|
|
|
96 |
# Ensure NLTK uses the correct data path
|
97 |
nltk.data.path.append(os.getenv('NLTK_DATA'))
|
98 |
|
99 |
+
# Define the device if using GPU
|
100 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
101 |
+
|
102 |
app = Flask(__name__)
|
103 |
|
104 |
# Ensure the Transformers cache directory is set correctly
|
|
|
151 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
152 |
|
153 |
gen_kwargs = {"length_penalty": 0.8, "num_beams": 8, "max_length": 128}
|
154 |
+
pipe = pipeline("summarization", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
|
155 |
|
156 |
text = pipe(input_text, **gen_kwargs)[0]["summary_text"]
|
157 |
output_text = replace_pronouns(remove_spaces_before_punctuation(text))
|
158 |
|
159 |
# Clear the GPU cache
|
160 |
+
if device == "cuda":
|
161 |
+
torch.cuda.empty_cache()
|
162 |
|
163 |
# Return the summary
|
164 |
return jsonify({'summary': output_text})
|
|
|
169 |
|
170 |
if __name__ == '__main__':
|
171 |
app.run(host='0.0.0.0', debug=True, port=7860)
|
172 |
+
|