Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,25 +21,28 @@ task_instructions = {
|
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def preprocess_text(text):
|
| 26 |
"""
|
| 27 |
-
Clean and validate the user's input text.
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
# Detect input language
|
| 31 |
language = langdetect.detect(text)
|
| 32 |
if language != "en":
|
| 33 |
return f"Input language detected as {language}. Please provide input in English."
|
| 34 |
-
except
|
| 35 |
return "Unable to detect language. Please provide valid text input."
|
|
|
|
|
|
|
|
|
|
| 36 |
return text.strip()
|
| 37 |
|
| 38 |
|
| 39 |
-
#
|
| 40 |
def respond(task, message, history, system_message, max_tokens, temperature, top_p):
|
| 41 |
"""
|
| 42 |
-
Handle user messages and generate responses using the NLP model.
|
| 43 |
"""
|
| 44 |
# Apply task-specific instructions
|
| 45 |
system_message = f"{system_message} Task: {task_instructions.get(task, 'General NLP task')}"
|
|
@@ -47,8 +50,7 @@ def respond(task, message, history, system_message, max_tokens, temperature, top
|
|
| 47 |
# Preprocess the user's input
|
| 48 |
message = preprocess_text(message)
|
| 49 |
if message.startswith("Input language detected") or message.startswith("Unable to detect"):
|
| 50 |
-
|
| 51 |
-
return
|
| 52 |
|
| 53 |
# Prepare conversation history
|
| 54 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -59,10 +61,10 @@ def respond(task, message, history, system_message, max_tokens, temperature, top
|
|
| 59 |
messages.append({"role": "assistant", "content": assistant_message})
|
| 60 |
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
-
response = ""
|
| 63 |
|
| 64 |
-
# Stream response from the Hugging Face model
|
| 65 |
try:
|
|
|
|
| 66 |
for chunk in client.chat_completion(
|
| 67 |
messages=messages,
|
| 68 |
max_tokens=max_tokens,
|
|
@@ -77,33 +79,37 @@ def respond(task, message, history, system_message, max_tokens, temperature, top
|
|
| 77 |
yield f"Error generating response: {str(e)}"
|
| 78 |
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
def save_history(history):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
-
|
| 88 |
-
def load_history():
|
| 89 |
try:
|
| 90 |
-
with open(
|
| 91 |
history = json.load(f)
|
| 92 |
return history
|
| 93 |
except FileNotFoundError:
|
| 94 |
return []
|
|
|
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
-
#
|
| 98 |
def create_interface():
|
| 99 |
"""
|
| 100 |
-
Create the Gradio interface for the chatbot.
|
| 101 |
"""
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
gr.Markdown("## 🧠 NLPToolkit Agent\nAn advanced assistant for NLP tasks, powered by Hugging Face.")
|
| 104 |
|
|
|
|
| 105 |
with gr.Row():
|
| 106 |
-
# Task selection dropdown
|
| 107 |
task = gr.Dropdown(
|
| 108 |
choices=["Summarization", "Sentiment Analysis", "Text Classification", "Entity Recognition"],
|
| 109 |
value="Summarization",
|
|
@@ -111,36 +117,32 @@ def create_interface():
|
|
| 111 |
)
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
-
# User input and system message
|
| 115 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 116 |
system_message = gr.Textbox(value=default_system_message, label="System Message")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
-
# Chat history and assistant response
|
| 120 |
chat_history = gr.State(value=[])
|
| 121 |
assistant_response = gr.Textbox(label="Assistant Response", interactive=False)
|
| 122 |
|
| 123 |
with gr.Row():
|
| 124 |
-
# Parameter sliders
|
| 125 |
max_tokens = gr.Slider(1, 2048, value=512, label="Max Tokens")
|
| 126 |
temperature = gr.Slider(0.1, 4.0, value=0.7, label="Temperature")
|
| 127 |
top_p = gr.Slider(0.1, 1.0, value=0.95, label="Top-p (Nucleus Sampling)")
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
-
# Buttons for save/load functionality
|
| 131 |
save_button = gr.Button("Save Chat History")
|
| 132 |
load_button = gr.Button("Load Chat History")
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
-
# Submit button
|
| 136 |
submit_button = gr.Button("Generate Response")
|
| 137 |
|
| 138 |
-
# Connect
|
| 139 |
submit_button.click(
|
| 140 |
fn=respond,
|
| 141 |
inputs=[task, user_input, chat_history, system_message, max_tokens, temperature, top_p],
|
| 142 |
outputs=assistant_response
|
| 143 |
)
|
|
|
|
| 144 |
save_button.click(fn=save_history, inputs=chat_history, outputs=None)
|
| 145 |
load_button.click(fn=load_history, inputs=None, outputs=chat_history)
|
| 146 |
|
|
@@ -149,7 +151,7 @@ def create_interface():
|
|
| 149 |
return demo
|
| 150 |
|
| 151 |
|
| 152 |
-
# Run the app
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo = create_interface()
|
| 155 |
demo.launch()
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
+
# Enhanced text preprocessing function
|
| 25 |
def preprocess_text(text):
|
| 26 |
"""
|
| 27 |
+
Clean and validate the user's input text with better error handling and language detection.
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
# Detect input language
|
| 31 |
language = langdetect.detect(text)
|
| 32 |
if language != "en":
|
| 33 |
return f"Input language detected as {language}. Please provide input in English."
|
| 34 |
+
except langdetect.lang_detect_exception.LangDetectException:
|
| 35 |
return "Unable to detect language. Please provide valid text input."
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"An error occurred while processing the text: {str(e)}"
|
| 38 |
+
|
| 39 |
return text.strip()
|
| 40 |
|
| 41 |
|
| 42 |
+
# Enhanced respond function with better error handling and structured flow
|
| 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 improved error handling and response flow.
|
| 46 |
"""
|
| 47 |
# Apply task-specific instructions
|
| 48 |
system_message = f"{system_message} Task: {task_instructions.get(task, 'General NLP task')}"
|
|
|
|
| 50 |
# Preprocess the user's input
|
| 51 |
message = preprocess_text(message)
|
| 52 |
if message.startswith("Input language detected") or message.startswith("Unable to detect"):
|
| 53 |
+
return message # Early exit on language issues
|
|
|
|
| 54 |
|
| 55 |
# Prepare conversation history
|
| 56 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 61 |
messages.append({"role": "assistant", "content": assistant_message})
|
| 62 |
|
| 63 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 64 |
|
| 65 |
+
# Stream response from the Hugging Face model with improved error handling
|
| 66 |
try:
|
| 67 |
+
response = ""
|
| 68 |
for chunk in client.chat_completion(
|
| 69 |
messages=messages,
|
| 70 |
max_tokens=max_tokens,
|
|
|
|
| 79 |
yield f"Error generating response: {str(e)}"
|
| 80 |
|
| 81 |
|
| 82 |
+
# Improved chat history management functions with better file handling
|
| 83 |
+
def save_history(history, filename="chat_history.json"):
|
| 84 |
+
try:
|
| 85 |
+
with open(filename, "w") as f:
|
| 86 |
+
json.dump(history, f)
|
| 87 |
+
return "Chat history saved successfully."
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error saving chat history: {str(e)}"
|
| 90 |
|
| 91 |
|
| 92 |
+
def load_history(filename="chat_history.json"):
|
|
|
|
| 93 |
try:
|
| 94 |
+
with open(filename, "r") as f:
|
| 95 |
history = json.load(f)
|
| 96 |
return history
|
| 97 |
except FileNotFoundError:
|
| 98 |
return []
|
| 99 |
+
except json.JSONDecodeError:
|
| 100 |
+
return [] # Handle case where the file is empty or corrupt
|
| 101 |
|
| 102 |
|
| 103 |
+
# Refactor the Gradio interface to be more organized and responsive
|
| 104 |
def create_interface():
|
| 105 |
"""
|
| 106 |
+
Create and enhance the Gradio interface for the chatbot with improved layout and feedback.
|
| 107 |
"""
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
gr.Markdown("## 🧠 NLPToolkit Agent\nAn advanced assistant for NLP tasks, powered by Hugging Face.")
|
| 110 |
|
| 111 |
+
# Organize task selection and parameters in a better layout
|
| 112 |
with gr.Row():
|
|
|
|
| 113 |
task = gr.Dropdown(
|
| 114 |
choices=["Summarization", "Sentiment Analysis", "Text Classification", "Entity Recognition"],
|
| 115 |
value="Summarization",
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Row():
|
|
|
|
| 120 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 121 |
system_message = gr.Textbox(value=default_system_message, label="System Message")
|
| 122 |
|
| 123 |
with gr.Row():
|
|
|
|
| 124 |
chat_history = gr.State(value=[])
|
| 125 |
assistant_response = gr.Textbox(label="Assistant Response", interactive=False)
|
| 126 |
|
| 127 |
with gr.Row():
|
|
|
|
| 128 |
max_tokens = gr.Slider(1, 2048, value=512, label="Max Tokens")
|
| 129 |
temperature = gr.Slider(0.1, 4.0, value=0.7, label="Temperature")
|
| 130 |
top_p = gr.Slider(0.1, 1.0, value=0.95, label="Top-p (Nucleus Sampling)")
|
| 131 |
|
| 132 |
with gr.Row():
|
|
|
|
| 133 |
save_button = gr.Button("Save Chat History")
|
| 134 |
load_button = gr.Button("Load Chat History")
|
| 135 |
|
| 136 |
with gr.Row():
|
|
|
|
| 137 |
submit_button = gr.Button("Generate Response")
|
| 138 |
|
| 139 |
+
# Connect button actions and ensure smooth flow
|
| 140 |
submit_button.click(
|
| 141 |
fn=respond,
|
| 142 |
inputs=[task, user_input, chat_history, system_message, max_tokens, temperature, top_p],
|
| 143 |
outputs=assistant_response
|
| 144 |
)
|
| 145 |
+
|
| 146 |
save_button.click(fn=save_history, inputs=chat_history, outputs=None)
|
| 147 |
load_button.click(fn=load_history, inputs=None, outputs=chat_history)
|
| 148 |
|
|
|
|
| 151 |
return demo
|
| 152 |
|
| 153 |
|
| 154 |
+
# Run the enhanced Gradio app
|
| 155 |
if __name__ == "__main__":
|
| 156 |
demo = create_interface()
|
| 157 |
demo.launch()
|