Spaces:
Runtime error
Runtime error
File size: 1,453 Bytes
0140c70 |
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 |
import csv
import os
import gradio as gr
# GPT Prompt
PROMPTS_CSV_PATH = "saved_prompts.csv"
def get_prompts_from_csv():
if not os.path.exists(PROMPTS_CSV_PATH):
return []
with open(PROMPTS_CSV_PATH, 'r', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
# remove empty rows
return [row[0] for row in reader if row]
def save_prompt(prompt):
# Append CSV
with open(PROMPTS_CSV_PATH, 'a+', newline='', encoding='utf-8') as file:
# Move to start
file.seek(0)
reader = csv.reader(file)
existing_prompts = [row[0] for row in reader]
if prompt not in existing_prompts:
writer = csv.writer(file)
writer.writerow([prompt])
# Move to end
file.seek(0, os.SEEK_END)
return gr.Dropdown(label="Saved Prompts", choices=get_prompts_from_csv(), type="value", interactive=True)
def delete_prompt(prompt):
lines = []
with open(PROMPTS_CSV_PATH, 'r', newline='', encoding='utf-8') as readFile:
reader = csv.reader(readFile)
lines = [row for row in reader if row and row[0] != prompt]
with open(PROMPTS_CSV_PATH, 'w', newline='', encoding='utf-8') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
return gr.Dropdown(label="Saved Prompts", choices=get_prompts_from_csv(), type="value", interactive=True) |