Ghmustafa11 commited on
Commit
b7da222
·
verified ·
1 Parent(s): 5db717d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -1,28 +1,33 @@
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
- # Load the model and tokenizer (e.g., a GPT model from Hugging Face)
5
  @st.cache_resource
6
  def load_model():
7
- model_name = "microsoft/DialoGPT-small" # Replace with your chosen model
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name)
10
  return tokenizer, model
11
 
12
- # Initialize model and tokenizer
13
  tokenizer, model = load_model()
14
 
15
- # Streamlit app layout
16
- st.title("🤖 Friendly Chatbot")
17
- st.write("Hi! I'm your friendly chatbot. Ask me anything or just say hello! 😊")
18
 
19
  # User input
20
- user_input = st.text_input("You:", placeholder="Type your message here...")
21
 
22
- # Generate response
23
  if user_input:
 
24
  inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
25
- response = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
26
- bot_reply = tokenizer.decode(response[0], skip_special_tokens=True)
27
- st.text_area("Chatbot:", value=bot_reply, height=100)
 
 
28
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
+ # Load the model and tokenizer (optimized for caching in Streamlit)
5
  @st.cache_resource
6
  def load_model():
7
+ model_name = "microsoft/DialoGPT-small" # A conversational model from Hugging Face
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name)
10
  return tokenizer, model
11
 
12
+ # Load model and tokenizer
13
  tokenizer, model = load_model()
14
 
15
+ # Streamlit App Layout
16
+ st.title("🤖 Friendly English Chatbot")
17
+ st.subheader("Hi! I'm here to chat with you. Feel free to ask me anything or just say hello! 😊")
18
 
19
  # User input
20
+ user_input = st.text_input("Your Message:", placeholder="Type your message here...")
21
 
 
22
  if user_input:
23
+ # Encode the user's input and generate a response
24
  inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
25
+ response = model.generate(inputs, max_length=100, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
26
+ chatbot_reply = tokenizer.decode(response[0], skip_special_tokens=True)
27
+
28
+ # Display the chatbot's response
29
+ st.text_area("Chatbot Reply:", value=chatbot_reply, height=150)
30
 
31
+ # Footer
32
+ st.markdown("---")
33
+ st.caption("Powered by Hugging Face and Streamlit")