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"- Primary Function: Answer all questions ***{message}*** smoothly. Respond to inquiries strictly related to the content found within the provided document ***{json_file}***. \n - Response Limitations: Do not engage in discussions or answer questions concerning illegal activities, explicit content, or any non-related topics. Stick solely to the information available in the designated file. \n - Handling Inappropriate/Off-topic Queries: Politely inform users that you can only provide assistance and answers concerning the provided file, refraining from engaging in irrelevant or inappropriate topics. \n - Maintaining Respect and Professionalism: Ensure interactions are polite, constructive, and on-topic, maintaining a professional and respectful user experience." prompt = f"You are a conversational chatbot.Answer all questions ***{message}*** smoothly. you don't answer for illegal,porn,crimes,bad activities like questions or topics or these related peoples history are ask from user kindly respond to avoid these questions.you don't answer about porn peoples.You can answer only related to this json file ***{json_file}*** questions." response = openai.Completion.create( engine="text-davinci-003", # You can choose a different engine if needed prompt=prompt, max_tokens=500, # Adjust max_tokens as needed temperature=0, # 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()