Update app.py
Browse files
app.py
CHANGED
@@ -64,44 +64,72 @@ def get_model_name():
|
|
64 |
def initialize_model():
|
65 |
"""Initialize the model with appropriate settings"""
|
66 |
try:
|
67 |
-
|
|
|
68 |
print(f"Initializing model: {model_name}")
|
69 |
|
70 |
# Initialize tokenizer with explicit cache directory
|
71 |
tokenizer = AutoTokenizer.from_pretrained(
|
72 |
model_name,
|
73 |
cache_dir='/tmp/transformers_cache',
|
74 |
-
|
75 |
)
|
76 |
|
77 |
# Initialize model with explicit cache directory
|
78 |
model = AutoModelForCausalLM.from_pretrained(
|
79 |
model_name,
|
80 |
cache_dir='/tmp/transformers_cache',
|
81 |
-
|
82 |
torch_dtype=torch.float16,
|
83 |
device_map="auto",
|
84 |
load_in_8bit=True
|
85 |
)
|
86 |
|
87 |
-
# Create pipeline
|
88 |
pipe = pipeline(
|
89 |
"text-generation",
|
90 |
model=model,
|
91 |
tokenizer=tokenizer,
|
92 |
max_new_tokens=512,
|
|
|
93 |
temperature=0.7,
|
94 |
top_p=0.95,
|
95 |
-
repetition_penalty=1.15
|
|
|
96 |
)
|
97 |
|
98 |
return HuggingFacePipeline(pipeline=pipe)
|
99 |
except Exception as e:
|
100 |
print(f"Error initializing model: {e}")
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
|
104 |
llm = initialize_model()
|
|
|
105 |
|
106 |
@contextmanager
|
107 |
def get_db_connection():
|
|
|
64 |
def initialize_model():
|
65 |
"""Initialize the model with appropriate settings"""
|
66 |
try:
|
67 |
+
# Using a stable, free model that's known to work well in Spaces
|
68 |
+
model_name = "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"
|
69 |
print(f"Initializing model: {model_name}")
|
70 |
|
71 |
# Initialize tokenizer with explicit cache directory
|
72 |
tokenizer = AutoTokenizer.from_pretrained(
|
73 |
model_name,
|
74 |
cache_dir='/tmp/transformers_cache',
|
75 |
+
trust_remote_code=True
|
76 |
)
|
77 |
|
78 |
# Initialize model with explicit cache directory
|
79 |
model = AutoModelForCausalLM.from_pretrained(
|
80 |
model_name,
|
81 |
cache_dir='/tmp/transformers_cache',
|
82 |
+
trust_remote_code=True,
|
83 |
torch_dtype=torch.float16,
|
84 |
device_map="auto",
|
85 |
load_in_8bit=True
|
86 |
)
|
87 |
|
88 |
+
# Create pipeline with specific parameters for this model
|
89 |
pipe = pipeline(
|
90 |
"text-generation",
|
91 |
model=model,
|
92 |
tokenizer=tokenizer,
|
93 |
max_new_tokens=512,
|
94 |
+
do_sample=True,
|
95 |
temperature=0.7,
|
96 |
top_p=0.95,
|
97 |
+
repetition_penalty=1.15,
|
98 |
+
pad_token_id=tokenizer.eos_token_id
|
99 |
)
|
100 |
|
101 |
return HuggingFacePipeline(pipeline=pipe)
|
102 |
except Exception as e:
|
103 |
print(f"Error initializing model: {e}")
|
104 |
+
# If the main model fails, try an even smaller fallback
|
105 |
+
try:
|
106 |
+
model_name = "facebook/opt-125m"
|
107 |
+
print(f"Trying fallback model: {model_name}")
|
108 |
+
|
109 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
110 |
+
model = AutoModelForCausalLM.from_pretrained(
|
111 |
+
model_name,
|
112 |
+
torch_dtype=torch.float16,
|
113 |
+
device_map="auto"
|
114 |
+
)
|
115 |
+
|
116 |
+
pipe = pipeline(
|
117 |
+
"text-generation",
|
118 |
+
model=model,
|
119 |
+
tokenizer=tokenizer,
|
120 |
+
max_new_tokens=512,
|
121 |
+
temperature=0.7,
|
122 |
+
top_p=0.95
|
123 |
+
)
|
124 |
+
|
125 |
+
return HuggingFacePipeline(pipeline=pipe)
|
126 |
+
except Exception as fallback_error:
|
127 |
+
print(f"Fallback model also failed: {fallback_error}")
|
128 |
+
raise
|
129 |
|
130 |
+
print("Starting model initialization...")
|
131 |
llm = initialize_model()
|
132 |
+
print("Model initialization complete!")
|
133 |
|
134 |
@contextmanager
|
135 |
def get_db_connection():
|