app.py
CHANGED
@@ -61,8 +61,6 @@ CLASSIFICATION_MODELS = [
|
|
61 |
|
62 |
class LocalModelManager:
|
63 |
def __init__(self):
|
64 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
65 |
-
logger.info(f"Using device: {self.device}")
|
66 |
self.models = {}
|
67 |
self.tokenizers = {}
|
68 |
self.pipelines = {}
|
@@ -75,42 +73,36 @@ class LocalModelManager:
|
|
75 |
self.tokenizers[model_path] = AutoTokenizer.from_pretrained(model_path)
|
76 |
|
77 |
if task == "text-generation":
|
78 |
-
model = AutoModelForCausalLM.from_pretrained(
|
79 |
-
model_path,
|
80 |
-
torch_dtype=torch.float16,
|
81 |
-
device_map="auto"
|
82 |
-
)
|
83 |
self.pipelines[model_path] = pipeline(
|
84 |
"text-generation",
|
85 |
-
model=
|
86 |
-
tokenizer=self.tokenizers[model_path]
|
87 |
-
|
88 |
-
|
89 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
90 |
-
model_path,
|
91 |
device_map="auto"
|
92 |
)
|
|
|
93 |
self.pipelines[model_path] = pipeline(
|
94 |
"text-classification",
|
95 |
-
model=
|
96 |
-
tokenizer=self.tokenizers[model_path]
|
|
|
|
|
|
|
97 |
)
|
98 |
|
99 |
-
self.models[model_path] = model
|
100 |
logger.info(f"Model loaded successfully: {model_path}")
|
101 |
except Exception as e:
|
102 |
logger.error(f"Error loading model {model_path}: {str(e)}")
|
103 |
raise
|
104 |
|
105 |
-
@spaces.GPU
|
106 |
def _generate_text_sync(self, pipeline, text: str) -> str:
|
107 |
"""ๅๆ็ใชใใญในใ็ๆใฎๅฎ่ก"""
|
108 |
outputs = pipeline(
|
109 |
text,
|
110 |
max_new_tokens=100,
|
111 |
-
do_sample=
|
112 |
-
temperature=0.7,
|
113 |
-
top_p=0.9,
|
114 |
num_return_sequences=1
|
115 |
)
|
116 |
return outputs[0]["generated_text"]
|
@@ -126,7 +118,7 @@ class LocalModelManager:
|
|
126 |
logger.error(f"Error in text generation with {model_path}: {str(e)}")
|
127 |
raise
|
128 |
|
129 |
-
@spaces.GPU
|
130 |
def _classify_text_sync(self, pipeline, text: str) -> str:
|
131 |
"""ๅๆ็ใชใใญในใๅ้กใฎๅฎ่ก"""
|
132 |
result = pipeline(text)
|
|
|
61 |
|
62 |
class LocalModelManager:
|
63 |
def __init__(self):
|
|
|
|
|
64 |
self.models = {}
|
65 |
self.tokenizers = {}
|
66 |
self.pipelines = {}
|
|
|
73 |
self.tokenizers[model_path] = AutoTokenizer.from_pretrained(model_path)
|
74 |
|
75 |
if task == "text-generation":
|
|
|
|
|
|
|
|
|
|
|
76 |
self.pipelines[model_path] = pipeline(
|
77 |
"text-generation",
|
78 |
+
model=model_path,
|
79 |
+
tokenizer=self.tokenizers[model_path],
|
80 |
+
torch_dtype=torch.bfloat16,
|
81 |
+
trust_remote_code=True,
|
|
|
|
|
82 |
device_map="auto"
|
83 |
)
|
84 |
+
else: # classification
|
85 |
self.pipelines[model_path] = pipeline(
|
86 |
"text-classification",
|
87 |
+
model=model_path,
|
88 |
+
tokenizer=self.tokenizers[model_path],
|
89 |
+
torch_dtype=torch.bfloat16,
|
90 |
+
trust_remote_code=True,
|
91 |
+
device_map="auto"
|
92 |
)
|
93 |
|
|
|
94 |
logger.info(f"Model loaded successfully: {model_path}")
|
95 |
except Exception as e:
|
96 |
logger.error(f"Error loading model {model_path}: {str(e)}")
|
97 |
raise
|
98 |
|
99 |
+
@spaces.GPU
|
100 |
def _generate_text_sync(self, pipeline, text: str) -> str:
|
101 |
"""ๅๆ็ใชใใญในใ็ๆใฎๅฎ่ก"""
|
102 |
outputs = pipeline(
|
103 |
text,
|
104 |
max_new_tokens=100,
|
105 |
+
do_sample=False,
|
|
|
|
|
106 |
num_return_sequences=1
|
107 |
)
|
108 |
return outputs[0]["generated_text"]
|
|
|
118 |
logger.error(f"Error in text generation with {model_path}: {str(e)}")
|
119 |
raise
|
120 |
|
121 |
+
@spaces.GPU
|
122 |
def _classify_text_sync(self, pipeline, text: str) -> str:
|
123 |
"""ๅๆ็ใชใใญในใๅ้กใฎๅฎ่ก"""
|
124 |
result = pipeline(text)
|