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 = "COVID19_FAQ - COVID19_FAQ.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 | |
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 otherwise simply say 'i dont know'." | |
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().style(height=350) | |
msg = gr.Textbox() | |
clear = gr.ClearButton([msg, chatbot]) | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
demo.launch() | |