Aamir commited on
Commit
5acbd0d
·
verified ·
1 Parent(s): a0cd9a4

Create chatbot.app

Browse files
Files changed (1) hide show
  1. chatbot.app +66 -0
chatbot.app ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import random
5
+
6
+ # Title and UI Customization
7
+ st.set_page_config(page_title="AI Chatbot", page_icon="🤖", layout="wide")
8
+ st.markdown(
9
+ """
10
+ <style>
11
+ .main {background-color: #f5f7fa;}
12
+ .stButton button {background-color: #6200EE; color: white; border-radius: 12px;}
13
+ #chat-container {animation: fadeIn 2s ease-in;}
14
+ @keyframes fadeIn {from {opacity: 0;} to {opacity: 1;}}
15
+ </style>
16
+ """,
17
+ unsafe_allow_html=True,
18
+ )
19
+
20
+ # Load Hugging Face Model
21
+ def load_model():
22
+ model_name = "gpt2" # Replace with your preferred Hugging Face model
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
24
+ model = AutoModelForCausalLM.from_pretrained(model_name)
25
+ return tokenizer, model
26
+
27
+ tokenizer, model = load_model()
28
+
29
+ # Store Chat History
30
+ if "chat_history" not in st.session_state:
31
+ st.session_state.chat_history = []
32
+
33
+ # User Interface
34
+ def chatbot_ui():
35
+ st.markdown(
36
+ """
37
+ <div id="chat-container" style="text-align: center;">
38
+ <h1>🚀 AI Chatbot</h1>
39
+ <p>Ask me anything, and I'll do my best to help you!</p>
40
+ </div>
41
+ """,
42
+ unsafe_allow_html=True,
43
+ )
44
+ user_input = st.text_input(🔍"Type your question:", "")
45
+
46
+ if st.button("Send") and user_input:
47
+ generate_response(user_input)
48
+
49
+ # Display Chat History
50
+ for i, chat in enumerate(st.session_state.chat_history):
51
+ if chat['role'] == 'user':
52
+ message(chat['content'], is_user=True, key=f'user_{i}')
53
+ else:
54
+ message(chat['content'], key=f'bot_{i}')
55
+
56
+ # Generate Response
57
+ def generate_response(user_input):
58
+ inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
59
+ outputs = model.generate(inputs, max_length=200, num_return_sequences=1, do_sample=True, temperature=0.7)
60
+ bot_reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
+
62
+ # Update Chat History
63
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
64
+ st.session_state.chat_history.append({"role": "bot", "content": bot_reply})
65
+
66
+ chatbot_ui()