File size: 4,105 Bytes
3ed0655 45a4c6f d42513f d42b0fc d42513f 30b5d3c bc3866d df8f649 d42b0fc 3ed0655 d42b0fc 3ed0655 ce15570 3ed0655 ec70109 f2ade1c 3ed0655 f2ade1c 3ed0655 | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import gradio as gr
#from gtts import gTTS
# Global variables
chat_histories = {}
current_chat_id = 0
# object creation
css = """footer {visibility: hidden}
.gradio-container {background: url('https://i0.wp.com/www.flexpowersystems.com/wp-content/uploads/2020/08/shutterstock_1251830596-scaled-1.jpg');repeat 0 0;}
"""
with gr.Blocks(theme=gr.themes.Soft(),css=css) as demo:
#https://i.gifer.com/J4o.gif
#https://i0.wp.com/www.flexpowersystems.com/wp-content/uploads/2020/08/shutterstock_1251830596-scaled-1.jpg
#https://static.wixstatic.com/media/5cfe14_30977ca5f4d04cc2a8977a980baf19a9~mv2.gif
with gr.Row(visible=False) as login:
with gr.Column(2):
user = gr.Textbox(label="Username",visible=False)
with gr.Column(1):
gr.HTML(value="<br><br><br><br><center><h1> Welcome </h1></center>")
user = gr.Textbox(label="Username")
password = gr.Textbox(label="password")
submit_btn = gr.Button("Signin")
with gr.Column(2):
user = gr.Textbox(label="Username",visible=False)
with gr.Column() as main:
#with gr.Accordion("See Details"):
#gr.Markdown("hhhhhhh")
with gr.Tab("Chatbot"):
with gr.Row():
# First Column with 20% width
with gr.Column(scale=1):
save_button = gr.Button("Start new Chat")
display_chat_names = gr.Textbox(label="Previous Chat histories",placeholder="Chat names not found", lines=17)
dropdown = gr.Dropdown(label="Select to load Previous chat",choices=["no chat found"])
# Second Column with 80% width
with gr.Column(scale=6):
chatbot = gr.Chatbot([[None,"Hi there, what brings you here today?"]],avatar_images=("https://cdnl.iconscout.com/lottie/premium/thumb/user-profile-5568736-4644453.gif","https://cdn.dribbble.com/users/77598/screenshots/16399264/media/d86ceb1ad552398787fb76f343080aa6.gif"),height=460,show_label=False,show_copy_button=True,show_share_button=True,likeable=True,layout="bubble")
with gr.Tab("text input (after typing press enter)"):
msg = gr.Textbox(show_label=False,placeholder="type anything and press enter")
with gr.Tab("voice input"):
audio_input = gr.Microphone(label="Press start record to speak", type="filepath")
with gr.Tab("Setup your own voice"):
audio_input = gr.Microphone(label="Press start record to speak and add your voice", type="filepath")
with gr.Tab("Select the language"):
dropdown = gr.Dropdown(label="select language",choices=["english"])
def submit(t):
return gr.Column(visible=True),gr.Row(visible=False)
submit_btn.click(submit,audio_input,[main,login])
def respond(message, chat_history):
# Basic echo response
chat_history.append((message, "message"))
return "", chat_history
def save_chat(chat_history):
global current_chat_id
current_chat_id += 1
chat_name = f"chat history {current_chat_id}"
chat_histories[chat_name] = chat_history
chat_names = "\n".join(chat_histories.keys())
print(chat_names.split("\n"))
new_choices = gr.Dropdown(choices = chat_names.split("\n"),label = "just Select to load chat")
return str(chat_names),[[None,"Hi there, what brings you here today?"]],new_choices
def load_chat(entered_chat_name):
# Fetch the specific chat history based on provided name
chat_name = entered_chat_name
print(chat_name)# Remove any trailing or leading spaces
if chat_name in chat_histories:
return "", chat_histories[chat_name]
else:
return "Chat not found!", []
dropdown.select(load_chat, [dropdown], outputs=[msg,chatbot])
msg.submit(respond, [msg, chatbot], [msg, chatbot])
save_button.click(save_chat, [chatbot], outputs=[display_chat_names,chatbot,dropdown]) # Added display_chat_names as an output
demo.launch()
|