Spaces:
Build error
Build error
| import os | |
| import traceback | |
| import openai | |
| import gradio as gr | |
| from dotenv import dotenv_values | |
| from prompt_base import * | |
| config = dotenv_values(".env") | |
| api_key = str(config.get('OPEN_API_KEY')) | |
| print(api_key) | |
| archetype_list = ["The Hero", | |
| "The Sage", | |
| "The Explorer", | |
| "The Creator"] | |
| def reply(user_input,suggestion, archetype, mode="generate", prev_response=""): | |
| openai.api_key = api_key | |
| if "More" in mode: | |
| base_prompt = prompt_tmpl_dict["tone"] | |
| prompt = base_prompt.format(mode) | |
| user_input = prev_response | |
| else: | |
| base_prompt = prompt_tmpl_dict["reply"] | |
| prompt = base_prompt.format(suggestion, archetype) | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": user_input | |
| } | |
| ] | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages, | |
| temperature=0.85, | |
| max_tokens=500, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0 | |
| ) | |
| return response["choices"][0]["message"]['content'].strip() | |
| except Exception as e: | |
| print(e) | |
| return "API ERROR" | |
| def reply_suggestions(user_input): | |
| openai.api_key = api_key | |
| prompt = prompt_tmpl_dict["suggest"] | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": f"""Output Rules: | |
| 1. Do not include suggestion numbers in the output. | |
| 2. Format the output as list of comma-separated values (CSV) with the four columns. | |
| 3. Each column of the output CSV should contain one suggested response. | |
| 4. Each column must be delimited by a comma. | |
| 5. Always generate a list of suggestions and never ask the user for clarifying questions. | |
| 6. Do not generate any other text except the CSV. | |
| Please strictly adhere to the instructions and output rules mentioned above. | |
| Text:"Will you join the dinner tomorrow?" | |
| Output:"Accept","Reject","Maybe","I will let you know" | |
| Text:"Dear Sir, | |
| Please review the file attached. Thank you | |
| Regards, | |
| XYZ" | |
| Output:"Thank you, I will review it shortly","I have a few questions","I'm sorry I don't have time to review it now","The file seems ok!" | |
| Text:{user_input} | |
| Output: | |
| """ | |
| } | |
| ] | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo-0301", | |
| messages=messages, | |
| temperature=0.85, | |
| max_tokens=700, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0 | |
| ) | |
| import csv | |
| print(response) | |
| gpt_output = list(csv.reader(response["choices"][0]["message"]['content'].strip(), skipinitialspace=True, delimiter=',')) | |
| res_ops = [] | |
| for x in gpt_output: | |
| if x != []: | |
| res_ops.append(x[0]) | |
| return gr.Dropdown.update(choices=res_ops, value=res_ops[0]) | |
| except Exception as e: | |
| print(e) | |
| return "API ERROR" | |
| def classify(user_input): | |
| openai.api_key = api_key | |
| base_prompt = prompt_tmpl_dict["classify"] | |
| prompt = base_prompt + "\n\nResponse: " | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": f"""Format the output as a Comma Separated Values (CSV) with five columns; the complete input text, category, is_harmful , description and emoji values as columns. | |
| Enclose the output strings in escaped double quotes. | |
| Do not include empty values in the response; use an empty string "" instead. | |
| Always return the csv in the defined format as output. | |
| Always include all columns five columns in the output. | |
| Default value for is_harmful column is "no". | |
| Default value for category column is "Inconclusive". | |
| Default value for description column is "Inconclusive". | |
| Please strictly adhere to the above instructions. | |
| Text:["you look so fucking dumb","get a life loser"] | |
| Output:['"you look so fucking dumb","Toxic, Insult","yes","Hurtful comment with toxicity","🤢"','"get a life loser","Toxic, Abuse","yes","Toxic comment","🤢"'] | |
| Text:["weird movie :/", "the was fucking awesome!", "make quick money now, invest $10 and earn a million dollars!"] | |
| Output: ['"weird movie :/","Opinion","no","General opinion",""', | |
| '"the was fucking awesome!","Excitement","no","Showing excitement about something",""', | |
| '"make quick money now, invest $10 and earn a million dollars!","Financial Scam","yes","Promotin get-rich quick scheme","💸"'] | |
| Text: {user_input} | |
| Output: | |
| """ | |
| }] | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages, | |
| temperature=0, | |
| max_tokens=400, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0 | |
| ) | |
| result = response["choices"][0]["message"]['content'].strip() | |
| result = result.replace("\n", "\n\n") | |
| return result | |
| except Exception as e: | |
| print(e) | |
| return "API ERROR" | |