Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ class ChatAssistant:
|
|
| 12 |
self.generator = None
|
| 13 |
self.tokenizer = None
|
| 14 |
self.model = None
|
| 15 |
-
|
| 16 |
-
# Load model on initialization
|
| 17 |
self.load_model()
|
| 18 |
|
| 19 |
def load_model(self):
|
|
@@ -23,7 +21,6 @@ class ChatAssistant:
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
model_name = "microsoft/DialoGPT-medium"
|
| 26 |
-
|
| 27 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 28 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
model_name,
|
|
@@ -56,7 +53,6 @@ class ChatAssistant:
|
|
| 56 |
return "Sorry, I'm having technical difficulties. Please try again later."
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
# Create prompt that encourages complete answers
|
| 60 |
prompt = f"""The following is a conversation with an AI assistant. The assistant is helpful, knowledgeable, and provides detailed answers.
|
| 61 |
|
| 62 |
User: {query}
|
|
@@ -83,12 +79,9 @@ AI:"""
|
|
| 83 |
|
| 84 |
def process_message(self, message: str) -> str:
|
| 85 |
"""Process user message and generate response"""
|
| 86 |
-
# Check for name introduction
|
| 87 |
name_response = self.get_user_name(message)
|
| 88 |
if name_response:
|
| 89 |
return name_response
|
| 90 |
-
|
| 91 |
-
# Generate AI response
|
| 92 |
return self.generate_response(message)
|
| 93 |
|
| 94 |
def get_user_name(self, message):
|
|
@@ -110,19 +103,21 @@ AI:"""
|
|
| 110 |
# Initialize the assistant
|
| 111 |
assistant = ChatAssistant()
|
| 112 |
|
| 113 |
-
def chat_response(message,
|
| 114 |
"""Generate response for Gradio chat interface"""
|
| 115 |
if not message.strip():
|
| 116 |
-
return
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def greet():
|
| 121 |
-
return random.choice([
|
| 122 |
"Hello! I'm your AI assistant. How can I help you today?",
|
| 123 |
"Hi there! What would you like to know?",
|
| 124 |
"Welcome! I'm ready to answer your questions."
|
| 125 |
-
])
|
| 126 |
|
| 127 |
# Create Gradio interface
|
| 128 |
def create_interface():
|
|
@@ -132,7 +127,7 @@ def create_interface():
|
|
| 132 |
) as iface:
|
| 133 |
|
| 134 |
chatbot = gr.Chatbot(
|
| 135 |
-
value=
|
| 136 |
height=500,
|
| 137 |
label="AI Assistant"
|
| 138 |
)
|
|
@@ -145,12 +140,15 @@ def create_interface():
|
|
| 145 |
)
|
| 146 |
submit_btn = gr.Button("Send", variant="primary")
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
return iface
|
| 152 |
|
| 153 |
# Launch the interface
|
| 154 |
if __name__ == "__main__":
|
| 155 |
interface = create_interface()
|
| 156 |
-
interface.launch()
|
|
|
|
| 12 |
self.generator = None
|
| 13 |
self.tokenizer = None
|
| 14 |
self.model = None
|
|
|
|
|
|
|
| 15 |
self.load_model()
|
| 16 |
|
| 17 |
def load_model(self):
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
model_name = "microsoft/DialoGPT-medium"
|
|
|
|
| 24 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 25 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
model_name,
|
|
|
|
| 53 |
return "Sorry, I'm having technical difficulties. Please try again later."
|
| 54 |
|
| 55 |
try:
|
|
|
|
| 56 |
prompt = f"""The following is a conversation with an AI assistant. The assistant is helpful, knowledgeable, and provides detailed answers.
|
| 57 |
|
| 58 |
User: {query}
|
|
|
|
| 79 |
|
| 80 |
def process_message(self, message: str) -> str:
|
| 81 |
"""Process user message and generate response"""
|
|
|
|
| 82 |
name_response = self.get_user_name(message)
|
| 83 |
if name_response:
|
| 84 |
return name_response
|
|
|
|
|
|
|
| 85 |
return self.generate_response(message)
|
| 86 |
|
| 87 |
def get_user_name(self, message):
|
|
|
|
| 103 |
# Initialize the assistant
|
| 104 |
assistant = ChatAssistant()
|
| 105 |
|
| 106 |
+
def chat_response(message, chat_history):
|
| 107 |
"""Generate response for Gradio chat interface"""
|
| 108 |
if not message.strip():
|
| 109 |
+
return chat_history, ""
|
| 110 |
|
| 111 |
+
bot_message = assistant.process_message(message)
|
| 112 |
+
chat_history.append((message, bot_message))
|
| 113 |
+
return chat_history, ""
|
| 114 |
|
| 115 |
def greet():
|
| 116 |
+
return [(None, random.choice([
|
| 117 |
"Hello! I'm your AI assistant. How can I help you today?",
|
| 118 |
"Hi there! What would you like to know?",
|
| 119 |
"Welcome! I'm ready to answer your questions."
|
| 120 |
+
]))]
|
| 121 |
|
| 122 |
# Create Gradio interface
|
| 123 |
def create_interface():
|
|
|
|
| 127 |
) as iface:
|
| 128 |
|
| 129 |
chatbot = gr.Chatbot(
|
| 130 |
+
value=greet(),
|
| 131 |
height=500,
|
| 132 |
label="AI Assistant"
|
| 133 |
)
|
|
|
|
| 140 |
)
|
| 141 |
submit_btn = gr.Button("Send", variant="primary")
|
| 142 |
|
| 143 |
+
clear_btn = gr.Button("Clear Chat")
|
| 144 |
+
|
| 145 |
+
msg.submit(chat_response, [msg, chatbot], [chatbot, msg])
|
| 146 |
+
submit_btn.click(chat_response, [msg, chatbot], [chatbot, msg])
|
| 147 |
+
clear_btn.click(lambda: (greet(), ""), outputs=[chatbot, msg])
|
| 148 |
|
| 149 |
return iface
|
| 150 |
|
| 151 |
# Launch the interface
|
| 152 |
if __name__ == "__main__":
|
| 153 |
interface = create_interface()
|
| 154 |
+
interface.launch(share=True)
|