sabahat-shakeel commited on
Commit
e97552d
·
verified ·
1 Parent(s): b74078f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -52
app.py CHANGED
@@ -1,62 +1,24 @@
1
- # import streamlit as st
2
- # from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
-
4
- # # Load the GPT-2 model and tokenizer
5
- # @st.cache_resource
6
- # def load_model():
7
- # model_name = "gpt2"
8
- # tokenizer = GPT2Tokenizer.from_pretrained(model_name)
9
- # model = GPT2LMHeadModel.from_pretrained(model_name)
10
- # return model, tokenizer
11
-
12
- # # Function to generate a response from GPT-2
13
- # def generate_response(input_text, model, tokenizer):
14
- # inputs = tokenizer.encode(input_text, return_tensors="pt")
15
- # outputs = model.generate(inputs, max_length=150, do_sample=True, top_p=0.9, top_k=50)
16
- # response = tokenizer.decode(outputs[0], skip_special_tokens=True)
17
- # return response
18
-
19
- # # Streamlit UI setup
20
- # def main():
21
- # st.title("GPT-2 Chatbot")
22
-
23
- # # Chat history
24
- # if 'history' not in st.session_state:
25
- # st.session_state['history'] = []
26
-
27
- # user_input = st.text_input("You:", "")
28
-
29
- # # Generate and display response
30
- # if user_input:
31
- # model, tokenizer = load_model()
32
- # response = generate_response(user_input, model, tokenizer)
33
- # st.session_state['history'].append({"user": user_input, "bot": response})
34
-
35
- # # Display chat history
36
- # for chat in st.session_state['history']:
37
- # st.write(f"You: {chat['user']}")
38
- # st.write(f"Bot: {chat['bot']}")
39
-
40
- # if __name__ == "__main__":
41
- # main()
42
  import streamlit as st
43
  from transformers import pipeline
44
 
45
- # Configure the Hugging Face API key
46
- HUGGINGFACE_API_KEY = st.secrets['huggingface_api_key']
 
47
 
48
- # Initialize the Hugging Face text-generation model (DialoGPT or other conversational models)
49
- chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", api_key=HUGGINGFACE_API_KEY)
50
 
51
- # Function to get response from the Hugging Face model
52
  def get_chatbot_response(user_input):
53
  try:
54
- response = chatbot(user_input, max_length=1000, pad_token_id=50256) # Generate response
55
- return response[0]['generated_text'] # Extract the generated response
 
56
  except Exception as e:
57
  return f"Error: {str(e)}"
58
 
59
- # Streamlit interface
60
  st.set_page_config(page_title="Smart ChatBot", layout="centered")
61
 
62
  # Custom CSS for chat bubbles with full width and emojis
@@ -88,7 +50,7 @@ st.markdown("""
88
  border-radius: 10px 10px 10px 10px;
89
  }
90
  .chat-header {
91
- # text-align: center;
92
  font-size: 35px;
93
  font-weight: bold;
94
  margin-bottom: 20px;
@@ -101,8 +63,8 @@ st.markdown("""
101
  </style>
102
  """, unsafe_allow_html=True)
103
 
104
- st.markdown('<div class="chat-header">Hugging Face Chatbot-Your AI Companion 💻</div>', unsafe_allow_html=True)
105
- st.write("Powered by Hugging Face for smart, engaging conversations. 🤖")
106
 
107
  if "history" not in st.session_state:
108
  st.session_state["history"] = []
 
1
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
  from transformers import pipeline
4
 
5
+ # Set the Hugging Face API key in the environment (if required)
6
+ HUGGINGFACE_API_KEY = st.secrets["huggingface_api_key"]
7
+ os.environ["HF_HOME"] = HUGGINGFACE_API_KEY # Set the Hugging Face API key
8
 
9
+ # Initialize the text generation pipeline (without passing api_key)
10
+ chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
11
 
12
+ # Function to get response from the model
13
  def get_chatbot_response(user_input):
14
  try:
15
+ # Generate response from the model
16
+ response = chatbot(user_input)
17
+ return response[0]["generated_text"]
18
  except Exception as e:
19
  return f"Error: {str(e)}"
20
 
21
+ # Streamlit interface setup
22
  st.set_page_config(page_title="Smart ChatBot", layout="centered")
23
 
24
  # Custom CSS for chat bubbles with full width and emojis
 
50
  border-radius: 10px 10px 10px 10px;
51
  }
52
  .chat-header {
53
+ text-align: center;
54
  font-size: 35px;
55
  font-weight: bold;
56
  margin-bottom: 20px;
 
63
  </style>
64
  """, unsafe_allow_html=True)
65
 
66
+ st.markdown('<div class="chat-header">Gemini Chatbot-Your AI Companion 💻</div>', unsafe_allow_html=True)
67
+ st.write("Powered by Hugging Face’s DialoGPT model for smart, engaging conversations. 🤖")
68
 
69
  if "history" not in st.session_state:
70
  st.session_state["history"] = []