import os import streamlit as st from transformers import pipeline # Set the Hugging Face API key in the environment (if required) HUGGINGFACE_API_KEY = st.secrets["huggingface_api_key"] os.environ["HF_HOME"] = HUGGINGFACE_API_KEY # Set the Hugging Face API key # Initialize the text generation pipeline (without passing api_key) chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium") # Initialize the conversation history if "history" not in st.session_state: st.session_state["history"] = [] # Function to get response from the model def get_chatbot_response(user_input): try: # Prepare the conversation history for the model conversation_history = "" for user_input, response in st.session_state["history"]: conversation_history += f"User: {user_input}\nBot: {response}\n" # Add the current user input to the conversation conversation_history += f"User: {user_input}\n" # Generate response from the model response = chatbot(conversation_history, max_length=1000, pad_token_id=50256)[0]["generated_text"] # Remove the user input from the generated response (optional) response = response[len(conversation_history):].strip() return response except Exception as e: return f"Error: {str(e)}" # Streamlit interface setup st.set_page_config(page_title="Smart ChatBot", layout="centered") # Custom CSS for chat bubbles with full width and emojis st.markdown(""" """, unsafe_allow_html=True) st.markdown('