Spaces:
Sleeping
Sleeping
Boning c
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
-
# Load the model and tokenizer from your Hugging Face Hub or Google Drive
|
5 |
-
model_name = "Smilyai-labs/Smily-ultra-1" # Replace with your model path
|
6 |
-
|
7 |
# Load the model and tokenizer
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# Function to generate the model's response
|
12 |
-
def chat_with_bot(user_input, chat_history):
|
13 |
-
# Prepare the chat history and user input
|
14 |
-
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
bot_input_ids = new_user_input_ids
|
18 |
-
|
19 |
-
|
20 |
-
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
|
21 |
|
22 |
# Generate a response from the model
|
23 |
-
chat_history_ids = model.generate(bot_input_ids, max_length=
|
24 |
-
|
25 |
-
|
26 |
-
bot_output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
27 |
-
chat_history += user_input + "\n" + bot_output + "\n"
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
chatbot
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
|
|
|
|
|
|
4 |
# Load the model and tokenizer
|
5 |
+
model = AutoModelForCausalLM.from_pretrained("Smilyai-labs/Smily-ultra-1")
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("Smilyai-labs/Smily-ultra-1")
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Function to generate responses from the model
|
9 |
+
def chatbot(input_text, chat_history=[]):
|
10 |
+
# Encode the new user input, add the chat history as context
|
11 |
+
new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt')
|
12 |
+
|
13 |
+
# Concatenate the chat history and the new user input
|
14 |
bot_input_ids = new_user_input_ids
|
15 |
+
for history in chat_history:
|
16 |
+
bot_input_ids = torch.cat([history['input_ids'], bot_input_ids], dim=-1)
|
|
|
17 |
|
18 |
# Generate a response from the model
|
19 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id,
|
20 |
+
temperature=0.7, top_k=50, top_p=0.95, do_sample=True,
|
21 |
+
eos_token_id=tokenizer.eos_token_id)
|
|
|
|
|
22 |
|
23 |
+
# Decode the generated response
|
24 |
+
bot_response = tokenizer.decode(chat_history_ids[0], skip_special_tokens=True)
|
25 |
+
|
26 |
+
# Update the chat history
|
27 |
+
chat_history.append({'input_ids': bot_input_ids, 'response': bot_response})
|
28 |
+
|
29 |
+
return bot_response, chat_history
|
30 |
+
|
31 |
+
# Gradio interface with a more chatbot-like layout
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
with gr.Column():
|
34 |
+
# Create a text box to display conversation history
|
35 |
+
chatbot_output = gr.Chatbot(label="Chatbot")
|
36 |
+
|
37 |
+
# Create a text box for user input at the bottom
|
38 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", show_label=False)
|
39 |
+
|
40 |
+
# Create a submit button to trigger the chatbot function
|
41 |
+
submit_button = gr.Button("Send")
|
42 |
+
|
43 |
+
# Link the button to the chatbot function
|
44 |
+
submit_button.click(chatbot, inputs=[user_input, chatbot_output], outputs=[chatbot_output, chatbot_output])
|
45 |
+
|
46 |
+
# Launch the Gradio interface
|
47 |
+
demo.launch()
|