Spaces:
Runtime error
Runtime error
Commit
·
a592fa2
1
Parent(s):
6d9e11a
more Storage
Browse files
app.py
CHANGED
|
@@ -1,21 +1,49 @@
|
|
| 1 |
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
|
| 2 |
import dat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the model and tokenizer
|
| 5 |
model_name = "LocalDoc/mbart_large_qa_azerbaijan"
|
| 6 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name, src_lang="en_XX", tgt_lang="az_AZ")
|
| 7 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def answer_question(context, question):
|
| 10 |
# Prepare input text
|
| 11 |
input_text = f"context: {context} question: {question}"
|
| 12 |
-
inputs = tokenizer(input_text, return_tensors="pt", max_length=
|
| 13 |
|
| 14 |
# Generate answer
|
| 15 |
outputs = model.generate(
|
| 16 |
input_ids=inputs["input_ids"],
|
| 17 |
attention_mask=inputs["attention_mask"],
|
| 18 |
-
max_length=
|
| 19 |
num_beams=5,
|
| 20 |
early_stopping=True
|
| 21 |
)
|
|
|
|
| 1 |
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
|
| 2 |
import dat
|
| 3 |
+
import os
|
| 4 |
+
import platform
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def setvar():
|
| 9 |
+
if platform.system() == "Windows":
|
| 10 |
+
print("Windows detected. Assigning cache directory to Transformers in AppData \ Local.")
|
| 11 |
+
transformers_cache_directory = os.path.join(os.getenv('LOCALAPPDATA'), 'transformers_cache')
|
| 12 |
+
if not os.path.exists(transformers_cache_directory):
|
| 13 |
+
try:
|
| 14 |
+
os.mkdir(transformers_cache_directory)
|
| 15 |
+
print(f"First launch. Directory '{transformers_cache_directory}' created successfully.")
|
| 16 |
+
except OSError as e:
|
| 17 |
+
print(f"Error creating directory '{transformers_cache_directory}': {e}")
|
| 18 |
+
else:
|
| 19 |
+
print(f"Directory '{transformers_cache_directory}' already exists.")
|
| 20 |
+
os.environ['TRANSFORMERS_CACHE'] = transformers_cache_directory
|
| 21 |
+
print("Environment variable assigned.")
|
| 22 |
+
del transformers_cache_directory
|
| 23 |
+
|
| 24 |
+
else:
|
| 25 |
+
print("Windows not detected. Assignment of Transformers cache directory not necessary.")
|
| 26 |
+
|
| 27 |
|
| 28 |
# Load the model and tokenizer
|
| 29 |
model_name = "LocalDoc/mbart_large_qa_azerbaijan"
|
| 30 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name, src_lang="en_XX", tgt_lang="az_AZ")
|
| 31 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 32 |
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
def answer_question(context, question):
|
| 38 |
# Prepare input text
|
| 39 |
input_text = f"context: {context} question: {question}"
|
| 40 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=5120000, truncation=False, padding="max_length")
|
| 41 |
|
| 42 |
# Generate answer
|
| 43 |
outputs = model.generate(
|
| 44 |
input_ids=inputs["input_ids"],
|
| 45 |
attention_mask=inputs["attention_mask"],
|
| 46 |
+
max_length=5120000,
|
| 47 |
num_beams=5,
|
| 48 |
early_stopping=True
|
| 49 |
)
|