Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import openai | |
from langdetect import detect | |
from gtts import gTTS | |
from pdfminer.high_level import extract_text | |
openai.api_key = os.environ['OPENAI_API_KEY'] | |
user_db = {os.environ['username1']: os.environ['password1'], os.environ['username2']: os.environ['password2'], os.environ['username3']: os.environ['password3']} | |
messages = [{"role": "system", "content": 'You are a helpful assistant.'}] | |
def roleChoice(role): | |
global messages | |
messages = [{"role": "system", "content": role}] | |
return "role:" + role | |
def audioGPT(audio): | |
global messages | |
audio_file = open(audio, "rb") | |
transcript = openai.Audio.transcribe("whisper-1", audio_file) | |
messages.append({"role": "user", "content": transcript["text"]}) | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
system_message = response["choices"][0]["message"] | |
messages.append(system_message) | |
chats = "" | |
for msg in messages: | |
if msg['role'] != 'system': | |
chats += msg['role'] + ": " + msg['content'] + "\n\n" | |
return chats | |
def textGPT(text): | |
global messages | |
messages.append({"role": "user", "content": text}) | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
system_message = response["choices"][0]["message"] | |
messages.append(system_message) | |
chats = "" | |
for msg in messages: | |
if msg['role'] != 'system': | |
chats += msg['role'] + ": " + msg['content'] + "\n\n" | |
return chats | |
def siriGPT(audio): | |
global messages | |
audio_file = open(audio, "rb") | |
transcript = openai.Audio.transcribe("whisper-1", audio_file) | |
messages.append({"role": "user", "content": transcript["text"]}) | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
system_message = response["choices"][0]["message"] | |
messages.append(system_message) | |
lang = detect(system_message['content']) | |
narrate_ans = gTTS(text=system_message['content'], lang=lang, slow=False) | |
narrate_ans.save("narrate.wav") | |
return "narrate.wav" | |
def fileGPT(prompt, file_obj): | |
global messages | |
file_text = extract_text(file_obj.name) | |
text = prompt + "\n\n" + file_text | |
messages.append({"role": "user", "content": text}) | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
system_message = response["choices"][0]["message"] | |
messages.append(system_message) | |
chats = "" | |
for msg in messages: | |
if msg['role'] != 'system': | |
chats += msg['role'] + ": " + msg['content'] + "\n\n" | |
return chats | |
def clear(): | |
global messages | |
messages = [{"role": "system", "content": 'You are a helpful technology assistant.'}] | |
return | |
def show(): | |
global messages | |
chats = "" | |
for msg in messages: | |
if msg['role'] != 'system': | |
chats += msg['role'] + ": " + msg['content'] + "\n\n" | |
return chats | |
with gr.Blocks() as chatHistory: | |
gr.Markdown("Click the Clear button below to remove all the chat history.") | |
clear_btn = gr.Button("Clear") | |
clear_btn.click(fn=clear, inputs=None, outputs=None, queue=False) | |
gr.Markdown("Click the Display button below to show all the chat history.") | |
show_out = gr.Textbox() | |
show_btn = gr.Button("Display") | |
show_btn.click(fn=show, inputs=None, outputs=show_out, queue=False) | |
role = gr.Interface(fn=roleChoice, inputs="text", outputs="text", description = "Choose your GPT roles, e.g. You are a helpful technology assistant. 你是一位 IT 架构师。 你是一位开发者关系顾问。你是一位机器学习工程师。你是一位高级 C++ 开发人员 ") | |
text = gr.Interface(fn=textGPT, inputs="text", outputs="text") | |
audio = gr.Interface(fn=audioGPT, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text") | |
siri = gr.Interface(fn=siriGPT, inputs=gr.Audio(source="microphone", type="filepath"), outputs = "audio") | |
file = gr.Interface(fn=fileGPT, inputs=["text", "file"], outputs="text", description = "Enter prompt sentences and your PDF. e.g. lets think step by step, summarize this following text: 或者 让我们一步一步地思考,总结以下的内容:") | |
demo = gr.TabbedInterface([role, text, audio, siri, file, chatHistory], [ "roleChoice", "chatGPT", "audioGPT", "siriGPT", "fileGPT", "ChatHistory"]) | |
if __name__ == "__main__": | |
demo.launch(enable_queue=False, auth=lambda u, p: user_db.get(u) == p, | |
auth_message="This is not designed to be used publicly as it links to a personal openAI API. However, you can copy my code and create your own multi-functional ChatGPT with your unique ID and password by utilizing the 'Repository secrets' feature in huggingface.") | |
#demo.launch() | |