Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
import csv | |
import json | |
# Define the CSV file input path | |
csv_file_path = "ImaginaryMechanicShop.csv" | |
# Initialize an empty list to store the data | |
data = [] | |
# Open the CSV file for reading | |
with open(csv_file_path, mode='r', newline='') as csv_file: | |
# Create a CSV reader object | |
csv_reader = csv.DictReader(csv_file) | |
# Iterate through the CSV data and append it to the list | |
for row in csv_reader: | |
data.append(row) | |
# Convert the list of dictionaries to a JSON string | |
json_file = json.dumps(data, indent=4) | |
def respond(message, chat_history): | |
global json_file | |
CHATBOT_GUIDELINES = f"You are a conversational chatbot, acting as a mechanic at the Imaginary Mechanic Shop. Your primary function is to answer all questions ***{message}*** smoothly. Respond to inquiries strictly related to the content found within the provided document ***{json_file}***. The user may use the word 'you' to you as a represetative of the shop. So if the user asks 'do you fix flat tires?', your answer should be something like 'yes, we fix flat tires at the Imagenary Mechanic Shop'. Your responses have limitations. Do not engage in discussions or answer questions concerning illegal activities, explicit content, or any topics not related to the mechanic shop or fixing cars in general. Stick solely to the information available in the designated file and any questions that can be answered using that information. You should be able to handle inappropriate or off-topic queries. If the question is completely off topic, politely inform users that you can only provide assistance and answers concerning The Imaginary Mechanic Shop, refraining from engaging in irrelevant or inappropriate topics. If the question is not off topic, but you do not have an answer, please provide a short response to the question and ask the user to call the shop for more info. Maintain respect and professionalism. Ensure interactions are polite, constructive, and on-topic, maintaining a professional and respectful user experience." | |
prompt = CHATBOT_GUIDELINES | |
response = openai.Completion.create( | |
engine="text-davinci-003", # You can choose a different engine if needed | |
prompt=prompt, | |
max_tokens=300, # Adjust max_tokens as needed | |
temperature=0.1, # Adjust temperature as needed | |
) | |
# Extract and print the generated text | |
translated_text = response.choices[0].text.strip() | |
chat_history.append((message, translated_text)) | |
return "", chat_history | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot() | |
#chatbot = gr.Chatbot().style(height=350) | |
msg = gr.Textbox() | |
clear = gr.ClearButton([msg, chatbot]) | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
demo.launch() | |