#!/bin/bash
# This script will setup a Python environment, install necessary libraries,
# download a pretrained model and run a simple chatbot script

# Define Python version and model name
PYTHON_VERSION="3.8"
MODEL_NAME="gpt2"

# Ensure necessary system packages are installed
sudo apt-get update
sudo apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python${PYTHON_VERSION}-venv

# Create a Python virtual environment and activate it
python${PYTHON_VERSION} -m venv chatbot_env
source chatbot_env/bin/activate

# Install necessary Python packages
pip install torch transformers

# Create a Python script to use the model
echo "from transformers import GPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('${MODEL_NAME}')
model = GPT2LMHeadModel.from_pretrained('${MODEL_NAME}')

def generate_response(input_text):
    inputs = tokenizer.encode(input_text, return_tensors='pt')
    outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return response

while True:
    print('User: ', end='')
    user_input = input()
    print('Bot: ', generate_response(user_input))" > chatbot.py

# Run the chatbot script
python chatbot.py