Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,8 +21,8 @@ task_instructions = {
|
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
-
# Enhanced text preprocessing function
|
| 25 |
-
def
|
| 26 |
"""
|
| 27 |
Clean and validate the user's input text with better error handling and language detection.
|
| 28 |
"""
|
|
@@ -39,16 +39,46 @@ def preprocess_text(text):
|
|
| 39 |
return text.strip()
|
| 40 |
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def respond(task, message, history, system_message, max_tokens, temperature, top_p):
|
| 44 |
"""
|
| 45 |
-
Handle user messages and generate responses using the NLP model with
|
| 46 |
"""
|
| 47 |
# Apply task-specific instructions
|
| 48 |
system_message = f"{system_message} Task: {task_instructions.get(task, 'General NLP task')}"
|
| 49 |
|
| 50 |
-
# Preprocess the user's input
|
| 51 |
-
message =
|
| 52 |
if message.startswith("Input language detected") or message.startswith("Unable to detect"):
|
| 53 |
return message # Early exit on language issues
|
| 54 |
|
|
@@ -62,21 +92,18 @@ def respond(task, message, history, system_message, max_tokens, temperature, top
|
|
| 62 |
|
| 63 |
messages.append({"role": "user", "content": message})
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
yield response
|
| 78 |
-
except Exception as e:
|
| 79 |
-
yield f"Error generating response: {str(e)}"
|
| 80 |
|
| 81 |
|
| 82 |
# Improved chat history management functions with better file handling
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
+
# Enhanced text preprocessing function (from pipeline)
|
| 25 |
+
def preprocess_input(text):
|
| 26 |
"""
|
| 27 |
Clean and validate the user's input text with better error handling and language detection.
|
| 28 |
"""
|
|
|
|
| 39 |
return text.strip()
|
| 40 |
|
| 41 |
|
| 42 |
+
# Model inference function (from pipeline)
|
| 43 |
+
def run_model_inference(messages, max_tokens, temperature, top_p):
|
| 44 |
+
"""
|
| 45 |
+
Run model inference based on the messages with specified parameters.
|
| 46 |
+
"""
|
| 47 |
+
try:
|
| 48 |
+
response = ""
|
| 49 |
+
for chunk in client.chat_completion(
|
| 50 |
+
messages=messages,
|
| 51 |
+
max_tokens=max_tokens,
|
| 52 |
+
stream=True,
|
| 53 |
+
temperature=temperature,
|
| 54 |
+
top_p=top_p,
|
| 55 |
+
):
|
| 56 |
+
token = chunk.choices[0].delta.content
|
| 57 |
+
response += token
|
| 58 |
+
yield response
|
| 59 |
+
except Exception as e:
|
| 60 |
+
yield f"Error generating response: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Output postprocessing (from pipeline)
|
| 64 |
+
def postprocess_output(response):
|
| 65 |
+
"""
|
| 66 |
+
Postprocess the model's response before presenting it to the user.
|
| 67 |
+
"""
|
| 68 |
+
# Example: Clean up the response or format it if necessary
|
| 69 |
+
return response.strip()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Enhanced respond function with pipeline integration
|
| 73 |
def respond(task, message, history, system_message, max_tokens, temperature, top_p):
|
| 74 |
"""
|
| 75 |
+
Handle user messages and generate responses using the NLP model with integrated pipeline.
|
| 76 |
"""
|
| 77 |
# Apply task-specific instructions
|
| 78 |
system_message = f"{system_message} Task: {task_instructions.get(task, 'General NLP task')}"
|
| 79 |
|
| 80 |
+
# Preprocess the user's input using the pipeline
|
| 81 |
+
message = preprocess_input(message)
|
| 82 |
if message.startswith("Input language detected") or message.startswith("Unable to detect"):
|
| 83 |
return message # Early exit on language issues
|
| 84 |
|
|
|
|
| 92 |
|
| 93 |
messages.append({"role": "user", "content": message})
|
| 94 |
|
| 95 |
+
# Get model response using the pipeline function (streamed)
|
| 96 |
+
response = ""
|
| 97 |
+
for chunk in run_model_inference(
|
| 98 |
+
messages=messages,
|
| 99 |
+
max_tokens=max_tokens,
|
| 100 |
+
temperature=temperature,
|
| 101 |
+
top_p=top_p
|
| 102 |
+
):
|
| 103 |
+
response = chunk
|
| 104 |
+
|
| 105 |
+
# Postprocess the model's response before sending it to the user
|
| 106 |
+
return postprocess_output(response)
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
# Improved chat history management functions with better file handling
|