This Gradio-based chatbot application utilizes the CodeLlama-34b-Instruct-hf model from Hugging Face for conversational interactions. It allows users to upload .txt or .docx files to provide additional context for the chatbot's responses.
- Chat Interface: A user-friendly chat interface powered by Gradio.
- Document Context: Users can upload
.txtor.docxfiles to provide context for the chatbot. - CodeLlama Model: Uses the CodeLlama-34b-Instruct-hf model for generating responses.
- Parameter Tuning: Users can adjust
max_tokensandtemperaturefor response generation. - Error Handling: Robust error handling for network issues, invalid inputs, and unexpected exceptions.
- Logging: Uses Python's
loggingmodule to log execution details and errors. - Environment Variables: Uses
.envfiles to securely manage the Hugging Face API key.
- Python 3.12
pip(Python package installer)- Hugging Face API key (stored in a
.envfile)
-
Clone the repository (if applicable):
git clone <your-repository-url> cd <your-repository-directory>
-
Install dependencies:
pip install gradio huggingface_hub python-docx python-dotenv
-
Create a
.envfile:- In the same directory as your Python script, create a file named
.env. - Add your Hugging Face API key to the
.envfile:HUGGINGFACE_API_KEY=your_huggingface_api_key- Replace
your_huggingface_api_keywith your actual API key.
- Replace
- In the same directory as your Python script, create a file named
-
Run the application:
python your_script_name.py
- Replace
your_script_name.pywith the actual name of your Python script.
- Replace
- Launch the application: Run the Python script.
- Chat with the chatbot: Enter your messages in the chat interface.
- Upload a file (optional): Upload a
.txtor.docxfile to provide additional context. - Adjust parameters: Use the sliders to adjust
max_tokensandtemperature. - View responses: The chatbot's responses will appear in the chat interface.
respond(current_msg, history_msg, max_tokens, temperature, file):- This function handles the chatbot's response generation.
- It takes the current message, chat history,
max_tokens,temperature, and uploaded file as input. - It constructs the system message, including any context from the uploaded file.
- It uses the Hugging Face
InferenceClientto interact with the CodeLlama model. - It handles potential errors and logs execution details.
chatbot = gr.ChatInterface(...):- This creates the Gradio chat interface.
- It specifies the
respondfunction as the chatbot's response generator. - It adds input components for
max_tokens,temperature, and file upload.
.envand API Key:- The
.envfile andload_dotenv()are used to manage the Hugging Face API key securely. - This prevents the API key from being hardcoded in the script.
- The
- File Handling:
- The code handles
.txtand.docxfiles, extracting the text content for context. - The
python-docxlibrary is used for.docxfiles.
- The code handles
- Logging:
- The
loggingmodule logs execution details and errors.
- The
gradiohuggingface_hubpython-docxpython-dotenv
- Ensure your Hugging Face API key is correctly set in the
.envfile. - The CodeLlama-34b-Instruct-hf model might require significant resources.
- Error handling is included, but further testing is recommended.
- The current time placeholder in the file output needs to be replaced with the actual current time implementation.