Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
|
| 18 |
try:
|
| 19 |
model = AutoModel.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
-
|
| 22 |
device_map="auto",
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
|
@@ -28,7 +28,7 @@ except KeyError:
|
|
| 28 |
model = AutoModel.from_pretrained(
|
| 29 |
MODEL_ID,
|
| 30 |
config=config,
|
| 31 |
-
|
| 32 |
device_map="auto",
|
| 33 |
trust_remote_code=True
|
| 34 |
)
|
|
@@ -44,7 +44,7 @@ def custom_generate(user_input):
|
|
| 44 |
lm_head = None
|
| 45 |
with torch.no_grad():
|
| 46 |
out = model(input_ids)
|
| 47 |
-
h = out.last_hidden_state if hasattr(out, "last_hidden_state") else out
|
| 48 |
dim = h.shape[-1]
|
| 49 |
|
| 50 |
for name, module in model.named_modules():
|
|
@@ -60,7 +60,7 @@ def custom_generate(user_input):
|
|
| 60 |
if not lm_head:
|
| 61 |
return "Error: Language head not found."
|
| 62 |
|
| 63 |
-
generated_ids = input_ids[0].tolist()
|
| 64 |
start_len = len(generated_ids)
|
| 65 |
|
| 66 |
# Generate tokens
|
|
@@ -68,7 +68,7 @@ def custom_generate(user_input):
|
|
| 68 |
curr_tensor = torch.tensor([generated_ids]).to(model.device)
|
| 69 |
with torch.no_grad():
|
| 70 |
out = model(curr_tensor)
|
| 71 |
-
h = out.last_hidden_state if hasattr(out, "last_hidden_state") else out
|
| 72 |
logits = lm_head(h[:, -1, :])
|
| 73 |
|
| 74 |
token = torch.argmax(logits, dim=-1).item()
|
|
@@ -78,19 +78,19 @@ def custom_generate(user_input):
|
|
| 78 |
break
|
| 79 |
|
| 80 |
response = tokenizer.decode(generated_ids[start_len:], skip_special_tokens=True)
|
| 81 |
-
return response.split("User:")[0].strip()
|
| 82 |
|
| 83 |
# Gradio Interface Chat Setup
|
| 84 |
def chat_interface(message, history):
|
| 85 |
return custom_generate(message)
|
| 86 |
|
| 87 |
-
# Launching Web UI
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
demo.launch()
|
|
|
|
| 18 |
try:
|
| 19 |
model = AutoModel.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
+
dtype=torch.bfloat16, # 'torch_dtype' warning fixed here
|
| 22 |
device_map="auto",
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
|
|
|
| 28 |
model = AutoModel.from_pretrained(
|
| 29 |
MODEL_ID,
|
| 30 |
config=config,
|
| 31 |
+
dtype=torch.bfloat16,
|
| 32 |
device_map="auto",
|
| 33 |
trust_remote_code=True
|
| 34 |
)
|
|
|
|
| 44 |
lm_head = None
|
| 45 |
with torch.no_grad():
|
| 46 |
out = model(input_ids)
|
| 47 |
+
h = out.last_hidden_state if hasattr(out, "last_hidden_state") else out
|
| 48 |
dim = h.shape[-1]
|
| 49 |
|
| 50 |
for name, module in model.named_modules():
|
|
|
|
| 60 |
if not lm_head:
|
| 61 |
return "Error: Language head not found."
|
| 62 |
|
| 63 |
+
generated_ids = input_ids[0].tolist() # Fixed slicing error here
|
| 64 |
start_len = len(generated_ids)
|
| 65 |
|
| 66 |
# Generate tokens
|
|
|
|
| 68 |
curr_tensor = torch.tensor([generated_ids]).to(model.device)
|
| 69 |
with torch.no_grad():
|
| 70 |
out = model(curr_tensor)
|
| 71 |
+
h = out.last_hidden_state if hasattr(out, "last_hidden_state") else out
|
| 72 |
logits = lm_head(h[:, -1, :])
|
| 73 |
|
| 74 |
token = torch.argmax(logits, dim=-1).item()
|
|
|
|
| 78 |
break
|
| 79 |
|
| 80 |
response = tokenizer.decode(generated_ids[start_len:], skip_special_tokens=True)
|
| 81 |
+
return response.split("User:")[0].strip() # Fixed string parsing logic here
|
| 82 |
|
| 83 |
# Gradio Interface Chat Setup
|
| 84 |
def chat_interface(message, history):
|
| 85 |
return custom_generate(message)
|
| 86 |
|
| 87 |
+
# Launching Web UI with explicit theme block to fix TypeError
|
| 88 |
+
with gr.Blocks(theme="soft") as demo:
|
| 89 |
+
gr.ChatInterface(
|
| 90 |
+
fn=chat_interface,
|
| 91 |
+
title="BOL-AI v1.0 PRO",
|
| 92 |
+
description="Developer: Vivek Vijay Dalvi | Company: MAHAVEER AI"
|
| 93 |
+
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
+
demo.launch()
|