Boning c commited on
Commit
467b01a
·
verified ·
1 Parent(s): 1c485d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
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
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name)
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
- # Combine chat history with the new input
 
 
 
 
 
17
  bot_input_ids = new_user_input_ids
18
- if chat_history:
19
- chat_history_ids = tokenizer.encode(chat_history + tokenizer.eos_token, return_tensors='pt')
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=1024, pad_token_id=tokenizer.eos_token_id)
24
-
25
- # Get the model's reply and update the chat history
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
- return chat_history, chat_history_ids
30
-
31
- # Set up Gradio interface
32
- chatbot = gr.Interface(
33
- fn=chat_with_bot,
34
- inputs=[
35
- gr.Textbox(label="User Input", placeholder="Type your message here..."),
36
- gr.State(value="")
37
- ],
38
- outputs=[
39
- gr.Textbox(label="Chat History"),
40
- gr.State(value="")
41
- ],
42
- live=True,
43
- title="Smily-ultra Chatbot",
44
- description="A simple chatbot powered by the Smily-ultra model",
45
- theme="compact"
46
- )
47
-
48
- # Launch the interface
49
- chatbot.launch(debug=True)
 
 
 
 
 
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()