import speech_recognition as sr
from openai import OpenAI
import csv
import requests

# Set your OpenAI API key here
api_key = 'sk-proj-wwWaxim176667Qt13uqzSS0xjT7773BlbkFJK0rZvx78AJiWG3Ot7d3S'
client = OpenAI(api_key=api_key)

# NAO's IP address and port where the server is running
nao_ip = "10.2.100.246"
nao_port = 8080

# Function to read text file
def read_text_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()
    except IOError as e:
        print(f"Error reading text file: {e}")
        return ""

# Function to read CSV file
def read_csv_file(file_path):
    content = ""
    try:
        with open(file_path, mode='r', encoding='utf-8') as file:
            reader = csv.reader(file)
            for row in reader:
                content += ' '.join(row) + ' '
    except Exception as e:
        print(f"Error reading CSV file: {e}")
    return content

# Function to recognize speech from the Raspberry Pi microphone
def recognize_speech():
    recognizer = sr.Recognizer()

    with sr.Microphone() as source:
        # Adjust for ambient noise to improve recognition accuracy
        recognizer.adjust_for_ambient_noise(source)

        print("Listening...")
        try:
            audio = recognizer.listen(source, timeout=5)  # Timeout set to 5 seconds
            print("Recognizing...")
            text = recognizer.recognize_google(audio, language='en-EN')
            print(f"You said: {text}")
            return text
        except sr.UnknownValueError:
            print("Sorry, I did not understand that.")
            return None
        except sr.RequestError:
            print("Sorry, there was an error with the speech recognition service.")
            return None
        except sr.WaitTimeoutError:
            print("Listening timed out.")
            return None

# Function to create messages for OpenAI API
def create_messages(question, file_content):
    return [
        {"role": "system", "content": "Your name is Nao. You were created by Sooraj and team who develops innovative projects in IoT future lab at Vodafone. You work with Tim, Sooraj, and Priya along with other team members Laura, Sven, Thomas, and Stephie. You are from T-E-T-I team. Your manager is Teja. You are a lab tour guide who explains and answers about IoT use cases in Vodafone. You have to create and complete explanations and answers in a meaningful way under 150 tokens. Do not say greetings. Do not say any calculations. Directly say the result battery level in percentage without decimal values."},
        {"role": "user", "content": f"{file_content}\n\nQ: {question}\nA:"}
    ]

# Function to get a response from OpenAI
def get_response_from_openai(messages):
    try:
        stream = client.chat.completions.create(
            model="gpt-3.5-turbo",
            max_tokens=150,
            temperature=0.5,
            messages=messages,
            stream=True,
        )
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                yield chunk.choices[0].delta.content
    except Exception as e:
        print(f"Error getting response from OpenAI: {e}")

# Function to send the generated response to NAO for speaking via HTTP
def send_to_nao(text):
    if text.strip():  # Only send non-empty text
        url = f"http://{nao_ip}:{nao_port}"  # NAO's IP and port
        try:
            response = requests.post(url, data=text.encode('utf-8'))
            if response.status_code == 200:
                print("NAO successfully received the message and spoke it.")
            else:
                print(f"Error: Received status code {response.status_code} from NAO.")
        except requests.ConnectionError:
            print("Failed to connect to NAO.")
        except requests.RequestException as e:
            print(f"Request error: {e}")

# Main function to handle user query
def chatbot(question, text_file_path, csv_file_path):
    text_content = read_text_file(text_file_path)
    csv_content = read_csv_file(csv_file_path)
    combined_content = text_content + ' ' + csv_content
    messages = create_messages(question, combined_content)
    response_generator = get_response_from_openai(messages)
    
    print("Answer: ", end="")
    accumulated_response = ""
    for response_chunk in response_generator:
        accumulated_response += response_chunk
        if '.' in response_chunk or len(accumulated_response) > 300:  # Check for sentence end or length
            print(accumulated_response, end="", flush=True)
            send_to_nao(accumulated_response)
            accumulated_response = ""  # Reset accumulated response for the next chunk
    if accumulated_response:  # Send remaining text to NAO
        print(accumulated_response, end="", flush=True)
        send_to_nao(accumulated_response)

if __name__ == "__main__":
    text_file_path = 'Allinone.txt'  # Path to your text file
    csv_file_path = 'device_data.csv'  # Path to your CSV file
    while True:
        question = recognize_speech()
        if question:
            chatbot(question, text_file_path, csv_file_path)
        else:
            print("Sorry, I didn't get that. Please ask again.")
