Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,16 +61,6 @@ config = AutoConfig.from_pretrained(REPO_NAME, token=HF_TOKEN)
|
|
| 61 |
|
| 62 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 63 |
|
| 64 |
-
#tokenizer = LlamaTokenizerFast.from_pretrained(REPO_NAME, token=HF_TOKEN)
|
| 65 |
-
#tokenizer = LlamaTokenizer.from_pretrained(REPO_NAME, token=HF_TOKEN)
|
| 66 |
-
|
| 67 |
-
#tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
|
| 68 |
-
# tokenizer = AutoTokenizer.from_pretrained(
|
| 69 |
-
# "microsoft/Phi-3-mini-4k-instruct",
|
| 70 |
-
# trust_remote_code=True,
|
| 71 |
-
# use_auth_token=HF_TOKEN
|
| 72 |
-
# )
|
| 73 |
-
|
| 74 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 75 |
|
| 76 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -95,6 +85,10 @@ def is_farewell(message: str) -> bool:
|
|
| 95 |
message_lower = message.lower().strip()
|
| 96 |
return any(farewell in message_lower for farewell in farewells)
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
@app.post("/chat")
|
| 99 |
async def chat(request: ChatRequest):
|
| 100 |
try:
|
|
@@ -119,13 +113,17 @@ async def chat(request: ChatRequest):
|
|
| 119 |
seen_messages = set()
|
| 120 |
for msg in history:
|
| 121 |
if msg.role == "user" and msg.text.strip() not in seen_messages:
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
elif msg.role == "model":
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
-
conversation += f"<|user|>\n{user_message.strip()}\n<|assistant|>"
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
inputs = tokenizer(conversation, return_tensors="pt", padding=True, truncation=True, max_length=4096).to(device)
|
| 130 |
|
| 131 |
with torch.no_grad():
|
|
|
|
| 61 |
|
| 62 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 65 |
|
| 66 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 85 |
message_lower = message.lower().strip()
|
| 86 |
return any(farewell in message_lower for farewell in farewells)
|
| 87 |
|
| 88 |
+
def clean_input_text(text: str) -> str:
|
| 89 |
+
# Remove any "Instruction N: ..." or similar phrases
|
| 90 |
+
return re.sub(r"Instruction\s*\d+\s*\(.*?\):", "", text, flags=re.IGNORECASE)
|
| 91 |
+
|
| 92 |
@app.post("/chat")
|
| 93 |
async def chat(request: ChatRequest):
|
| 94 |
try:
|
|
|
|
| 113 |
seen_messages = set()
|
| 114 |
for msg in history:
|
| 115 |
if msg.role == "user" and msg.text.strip() not in seen_messages:
|
| 116 |
+
cleaned_user_text = clean_input_text(msg.text.strip())
|
| 117 |
+
conversation += f"<|user|>\n{cleaned_user_text}\n"
|
| 118 |
+
seen_messages.add(cleaned_user_text)
|
| 119 |
elif msg.role == "model":
|
| 120 |
+
cleaned_model_text = clean_input_text(msg.text.strip())
|
| 121 |
+
conversation += f"<|assistant|>\n{cleaned_model_text}\n"
|
| 122 |
|
|
|
|
| 123 |
|
| 124 |
+
#conversation += f"<|user|>\n{user_message.strip()}\n<|assistant|>"
|
| 125 |
+
conversation += f"<|user|>\n{clean_input_text(user_message.strip())}\n<|assistant|>"
|
| 126 |
+
|
| 127 |
inputs = tokenizer(conversation, return_tensors="pt", padding=True, truncation=True, max_length=4096).to(device)
|
| 128 |
|
| 129 |
with torch.no_grad():
|