# GPTeam5 python script that performs GPT-3 based voice message summarization tasks

import tkinter as tk
from tkinter import filedialog
# import openai

# openai.api_key = ""
# key for the access to GPT-3 (keep private, do not share with others)


def ai_summary(prompt_main_req_base):
    # open.ai GPT-3 access that creates a summary
    response = openai.Completion.create(
      engine="davinci",
      prompt=prompt_main_req_base,
      temperature=0.25,
      max_tokens=120,
      top_p=1,
      frequency_penalty=0.4,
      presence_penalty=0,
      stop=["\n"]
    )
    return response.choices[0].text


def ai_sentiment(prompt_sentiment_req_base):
    # open.ai GPT-3 access for sentiment analysis
    response = openai.Completion.create(
      engine="curie",
      prompt=prompt_sentiment_req_base,
      temperature=0.1,
      max_tokens=6,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0,
      stop=["\n"]
    )
    return response.choices[0].text


root = tk.Tk()
root.iconbitmap(r"resources\logo.ico")
root.title("VOICE LINE")

voice_message_transcript = filedialog.askopenfilename(initialdir=r"..\GPTeam5_voiceline\vm_transcripts", title="VOICE LINE Voice Message Transcript Selection", filetypes=(("txt files", "*.txt"), ("all files", "*.*")))
# voice_message contains the file name (plus path) of the text file selected in the explorer window

root.destroy()

path = r"prompts\\"
prompt_main = "prompt_main.txt"
prompt_sentiment = "prompt_sentiment.txt"

file_prompt_main = open(path+prompt_main)
prompt_main_base = file_prompt_main.read()
file_prompt_main.close()
# MAIN Prompt from text file specified

file_prompt_sentiment = open(path+prompt_sentiment)
prompt_sentiment_base = file_prompt_sentiment.read()
file_prompt_sentiment.close()
# SENTIMENT Prompt from text file specified

file_text_main = open(voice_message_transcript)
message = file_text_main.read()
file_text_main.close()
# MAIN Message from voice to text conversion

message_main_base = "\n\nMessage: " + message + "\nSummary:"
prompt_main_req_base = prompt_main_base + message_main_base

message_sentiment_base = "\n\nMessage: " + message + "\nSentiment:"
prompt_sentiment_req_base = prompt_sentiment_base + message_sentiment_base

print("\n MESSAGE: " + message)

print("\n*** RESULTS generated by GPT3: ***\n")
output_main = ai_summary(prompt_main_req_base)
output_sentiment = ai_sentiment(prompt_sentiment_req_base)
# final output processed by GPT-3

print("SUMMARY: " + output_main)
print("SENTIMENT: " + output_sentiment)
