Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,64 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
|
| 4 |
|
| 5 |
# Load the chatbot model
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
-
model_name = "microsoft/DialoGPT-medium"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
return tokenizer, model
|
| 12 |
|
|
|
|
| 13 |
tokenizer, model = load_model()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def google_search(query):
|
| 17 |
-
params = {
|
| 18 |
-
"q": query,
|
| 19 |
-
"api_key": "YOUR_SERPAPI_KEY", # Replace with your SerpAPI key
|
| 20 |
-
}
|
| 21 |
-
search = GoogleSearch(params)
|
| 22 |
-
results = search.get_dict()
|
| 23 |
-
return results.get("organic_results", [])
|
| 24 |
-
|
| 25 |
-
# Chat history
|
| 26 |
if "chat_history" not in st.session_state:
|
| 27 |
st.session_state["chat_history"] = []
|
| 28 |
|
| 29 |
-
# Streamlit
|
| 30 |
-
st.title("
|
| 31 |
-
st.
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
|
|
|
| 35 |
if user_input:
|
| 36 |
# Add user input to chat history
|
| 37 |
-
st.session_state["chat_history"].append(f"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if search_results:
|
| 43 |
-
bot_reply = f"Here are the top Google results for your query:\n\n"
|
| 44 |
-
for idx, result in enumerate(search_results[:5], 1):
|
| 45 |
-
bot_reply += f"{idx}. [{result['title']}]({result['link']})\n"
|
| 46 |
-
else:
|
| 47 |
-
bot_reply = "Sorry, I couldn't find anything for your query."
|
| 48 |
-
else:
|
| 49 |
-
# Generate chatbot response
|
| 50 |
-
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
| 51 |
-
response = model.generate(inputs, max_length=500, pad_token_id=tokenizer.eos_token_id)
|
| 52 |
-
bot_reply = tokenizer.decode(response[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
for message in st.session_state["chat_history"]:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
# Reset
|
| 59 |
if st.button("Reset Chat"):
|
| 60 |
st.session_state["chat_history"] = []
|
| 61 |
st.write("Chat reset! Let's start fresh. π")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# Load the chatbot model
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
+
model_name = "microsoft/DialoGPT-medium" # Conversational model
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
return tokenizer, model
|
| 12 |
|
| 13 |
+
# Initialize model and tokenizer
|
| 14 |
tokenizer, model = load_model()
|
| 15 |
|
| 16 |
+
# Session state for chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if "chat_history" not in st.session_state:
|
| 18 |
st.session_state["chat_history"] = []
|
| 19 |
|
| 20 |
+
# Streamlit App Layout
|
| 21 |
+
st.title("π€ GM Chatbot")
|
| 22 |
+
st.markdown("Hi there! I'm **GM Chatbot**. I can answer your questions, chat with you, and keep the conversation friendly! π")
|
| 23 |
|
| 24 |
+
# User input
|
| 25 |
+
user_input = st.text_input("Your Message:", placeholder="Ask me anything or just say hello!")
|
| 26 |
|
| 27 |
+
# Chatbot Response Logic
|
| 28 |
if user_input:
|
| 29 |
# Add user input to chat history
|
| 30 |
+
st.session_state["chat_history"].append(f"You: {user_input}")
|
| 31 |
+
|
| 32 |
+
# Prepare input for the model
|
| 33 |
+
chat_history_str = "\n".join(st.session_state["chat_history"])
|
| 34 |
+
inputs = tokenizer.encode(chat_history_str + tokenizer.eos_token, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Generate chatbot response
|
| 37 |
+
response = model.generate(
|
| 38 |
+
inputs,
|
| 39 |
+
max_length=100,
|
| 40 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
top_p=0.9,
|
| 43 |
+
do_sample=True
|
| 44 |
+
)
|
| 45 |
+
bot_reply = tokenizer.decode(response[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 46 |
+
|
| 47 |
+
# Add bot reply to chat history
|
| 48 |
+
st.session_state["chat_history"].append(f"GM Chatbot: {bot_reply}")
|
| 49 |
+
|
| 50 |
+
# Display chat history
|
| 51 |
for message in st.session_state["chat_history"]:
|
| 52 |
+
if message.startswith("GM Chatbot"):
|
| 53 |
+
st.write(f"π€ {message}")
|
| 54 |
+
else:
|
| 55 |
+
st.write(f"π {message}")
|
| 56 |
|
| 57 |
+
# Reset Chat Button
|
| 58 |
if st.button("Reset Chat"):
|
| 59 |
st.session_state["chat_history"] = []
|
| 60 |
st.write("Chat reset! Let's start fresh. π")
|
| 61 |
+
|
| 62 |
+
# Footer
|
| 63 |
+
st.markdown("---")
|
| 64 |
+
st.caption("Powered by Hugging Face and Streamlit")
|