File size: 2,517 Bytes
148ef88
 
d38ee29
723b915
148ef88
 
 
 
48b4e88
148ef88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a028b2
6748cbf
148ef88
 
 
 
 
 
 
 
 
 
 
 
 
 
108a4bc
40ceffb
148ef88
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()