Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
#from gtts import gTTS
|
| 3 |
+
# Global variables
|
| 4 |
+
chat_histories = {}
|
| 5 |
+
current_chat_id = 0
|
| 6 |
+
# object creation
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
with gr.Blocks(theme=gr.themes.Soft(),css="footer {visibility: hidden}") as demo:
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
with gr.Row() as login:
|
| 13 |
+
with gr.Column(2):
|
| 14 |
+
user = gr.Textbox(label="Username",visible=False)
|
| 15 |
+
with gr.Column(1):
|
| 16 |
+
gr.HTML(value="<br><br><br><br><center><h1> Welcome </h1></center>")
|
| 17 |
+
|
| 18 |
+
user = gr.Textbox(label="Username")
|
| 19 |
+
password = gr.Textbox(label="password")
|
| 20 |
+
submit_btn = gr.Button("Signin")
|
| 21 |
+
with gr.Column(2):
|
| 22 |
+
user = gr.Textbox(label="Username",visible=False)
|
| 23 |
+
|
| 24 |
+
with gr.Column(visible=False) as main:
|
| 25 |
+
#with gr.Accordion("See Details"):
|
| 26 |
+
#gr.Markdown("hhhhhhh")
|
| 27 |
+
with gr.Tab("Chatbot"):
|
| 28 |
+
with gr.Row():
|
| 29 |
+
|
| 30 |
+
# First Column with 20% width
|
| 31 |
+
with gr.Column(scale=1):
|
| 32 |
+
save_button = gr.Button("Start new Chat")
|
| 33 |
+
display_chat_names = gr.Textbox(label="Chat histories",placeholder="Chat names not found", lines=16)
|
| 34 |
+
dropdown = gr.Dropdown(label="just Select to load chat",choices=["no chat found"])
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Second Column with 80% width
|
| 38 |
+
with gr.Column(scale=6):
|
| 39 |
+
|
| 40 |
+
chatbot = gr.Chatbot([[None,"Hi there, what brings you here today?"]],height=460,show_label=False,show_copy_button=True,show_share_button=True,likeable=True,layout="bubble")
|
| 41 |
+
with gr.Tab("text input"):
|
| 42 |
+
msg = gr.Textbox(show_label=False,placeholder="type anything")
|
| 43 |
+
with gr.Tab("voice input"):
|
| 44 |
+
audio_input = gr.Microphone(label="Press start record to speak", type="filepath")
|
| 45 |
+
|
| 46 |
+
with gr.Tab("Setting"):
|
| 47 |
+
audio_input = gr.Microphone(label="Press start recording to speak", type="filepath")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def submit(t):
|
| 51 |
+
return gr.Column(visible=True),gr.Row(visible=False)
|
| 52 |
+
submit_btn.click(submit,audio_input,[main,login])
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def respond(message, chat_history):
|
| 56 |
+
# Basic echo response
|
| 57 |
+
chat_history.append((message, "message"))
|
| 58 |
+
return "", chat_history
|
| 59 |
+
|
| 60 |
+
def save_chat(chat_history):
|
| 61 |
+
global current_chat_id
|
| 62 |
+
current_chat_id += 1
|
| 63 |
+
chat_name = f"chat history {current_chat_id}"
|
| 64 |
+
chat_histories[chat_name] = chat_history
|
| 65 |
+
chat_names = "\n".join(chat_histories.keys())
|
| 66 |
+
print(chat_names.split("\n"))
|
| 67 |
+
new_choices = gr.Dropdown(choices = chat_names.split("\n"),label = "just Select to load chat")
|
| 68 |
+
return str(chat_names),[[None,"Hi there, what brings you here today?"]],new_choices
|
| 69 |
+
|
| 70 |
+
def load_chat(entered_chat_name):
|
| 71 |
+
# Fetch the specific chat history based on provided name
|
| 72 |
+
chat_name = entered_chat_name
|
| 73 |
+
print(chat_name)# Remove any trailing or leading spaces
|
| 74 |
+
if chat_name in chat_histories:
|
| 75 |
+
return "", chat_histories[chat_name]
|
| 76 |
+
else:
|
| 77 |
+
return "Chat not found!", []
|
| 78 |
+
|
| 79 |
+
dropdown.select(load_chat, [dropdown], outputs=[msg,chatbot])
|
| 80 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 81 |
+
save_button.click(save_chat, [chatbot], outputs=[display_chat_names,chatbot,dropdown]) # Added display_chat_names as an output
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
demo.launch()
|