|
import gradio as gr |
|
import os |
|
import openai |
|
import subprocess |
|
import requests |
|
import json |
|
import csv |
|
import re |
|
|
|
def get_openai_api_key(): |
|
url = "https://neuroclubs.com/api/config.py" |
|
response = requests.get(url) |
|
|
|
if response.status_code == 200: |
|
config_content = response.text |
|
api_key_match = re.search(r'OPENAI_API_KEY\s*=\s*[\'"](.+?)[\'"]', config_content) |
|
if api_key_match: |
|
return api_key_match.group(1) |
|
else: |
|
raise Exception("Failed to extract OPENAI_API_KEY from config.py content") |
|
else: |
|
raise Exception("Failed to fetch config.py from remote server") |
|
|
|
openai.api_key = get_openai_api_key() |
|
|
|
prompt_templates = {"Default": ""} |
|
|
|
def get_empty_state(): |
|
return {"total_tokens": 0, "messages": []} |
|
|
|
def download_prompt_templates(): |
|
with open('promptsexchangeq.csv', newline='') as csvfile: |
|
reader = csv.reader(csvfile, delimiter=',', quotechar='"') |
|
next(reader) |
|
for row in reader: |
|
act, prompt = row |
|
prompt_templates[act] = prompt |
|
|
|
choices = list(prompt_templates.keys()) |
|
return gr.update(value=choices[0], choices=choices) |
|
|
|
def on_token_change(user_token): |
|
openai.api_key = config.OPENAI_API_KEY |
|
|
|
def on_prompt_template_change(prompt_template): |
|
if not isinstance(prompt_template, str): return |
|
return prompt_templates[prompt_template] |
|
|
|
def submit_message(user_token, prompt, prompt_template, temperature, max_tokens, state): |
|
|
|
history = state['messages'] |
|
|
|
if not prompt: |
|
return gr.update(value='', visible=state['total_tokens'] < 1_000), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: {state['total_tokens']} / 3000", state |
|
|
|
prompt_template = prompt_templates[prompt_template] |
|
|
|
system_prompt = [] |
|
if prompt_template: |
|
system_prompt = [{ "role": "system", "content": prompt_template }] |
|
|
|
prompt_msg = { "role": "user", "content": prompt } |
|
|
|
try: |
|
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history + [prompt_msg], temperature=temperature, max_tokens=max_tokens) |
|
|
|
history.append(prompt_msg) |
|
history.append(completion.choices[0].message.to_dict()) |
|
|
|
state['total_tokens'] += completion['usage']['total_tokens'] |
|
|
|
except Exception as e: |
|
history.append(prompt_msg) |
|
history.append({ |
|
"role": "system", |
|
"content": f"Error: {e}" |
|
}) |
|
|
|
total_tokens_used_msg = f"Total tokens used: {state['total_tokens']} / 3000" if not user_token else "" |
|
chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)] |
|
input_visibility = user_token or state['total_tokens'] < 3000 |
|
|
|
return gr.update(value='', visible=input_visibility), chat_messages, total_tokens_used_msg, state |
|
|
|
def clear_conversation(): |
|
return gr.update(value=None, visible=True), None, "", get_empty_state() |
|
|
|
css = """ |
|
#col-container {max-width: 80%; margin-left: auto; margin-right: auto;} |
|
#option-box {min-height: 250px; border-width: 2px; border-style: solid; border-color: #0000ff; border-radius: 4px;} |
|
#text-box {min-height: 70px; border-width: 2px; border-style: solid; border-color: #ff0000; border-radius: 4px;} |
|
#chatbox {min-height: 800px; border-width: 2px; border-style: solid; border-color: #008000; border-radius: 4px;} |
|
#header {text-align: center;} |
|
#prompt_template_preview {min-height: 250px; padding: 1em; border-width: 2px; border-style: solid; border-color: #23238E; border-radius: 4px;} |
|
#total_tokens_str {text-align: right; font-size: 0.8em; color: #666; height: 1em;} |
|
#clear-box {border-width: 2px; border-style: solid; border-color: #00ff00; border-radius: 4px;} |
|
.message { font-size: 1.2em; } |
|
""" |
|
|
|
with gr.Blocks(css=css) as demo: |
|
|
|
state = gr.State(get_empty_state()) |
|
|
|
with gr.Column(elem_id="option-box"): |
|
prompt_template = gr.Dropdown(label="Choose your prompt:", choices=list(prompt_templates.keys())) |
|
prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview", visible=False) |
|
user_token = gr.Textbox(placeholder="OpenAI API Key", type="password", show_label=False, visible=False) |
|
with gr.Accordion("Advanced parameters", open=False, visible=False): |
|
temperature = gr.Slider(value=0.5, top_p=1, frequency_penalty=0, presence_penalty=0.6, interactive=False, label="Temperature") |
|
max_tokens = gr.Slider(value=3000, interactive=False, label="Max tokens per response") |
|
|
|
with gr.Column(elem_id="text-box"): |
|
input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=True) |
|
btn_submit = gr.Button("Submit") |
|
|
|
with gr.Column(elem_id="chatbox"): |
|
chatbot = gr.Chatbot(elem_id="AI") |
|
total_tokens_str = gr.Markdown(elem_id="total_tokens_str") |
|
|
|
with gr.Column(elem_id="clear-box"): |
|
btn_clear_conversation = gr.Button("π Start New Conversation") |
|
|
|
|
|
btn_submit.click(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state]) |
|
input_message.submit(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state]) |
|
btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state]) |
|
prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview]) |
|
user_token.change(on_token_change, inputs=[user_token], outputs=[]) |
|
|
|
|
|
demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template]) |
|
|
|
|
|
demo.launch(debug=True) |
|
|