Create frontend.py
Browse files- frontend.py +85 -0
frontend.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
API_URL = "http://localhost:5000" # URL của Flask backend
|
5 |
+
|
6 |
+
def get_text_response(user_message, chat_history):
|
7 |
+
response = requests.post(f"{API_URL}/translate", json={"text": user_message})
|
8 |
+
translation = response.json()["translation"]
|
9 |
+
chat_history.append(("Người dùng", user_message))
|
10 |
+
chat_history.append(("Chatbot", translation))
|
11 |
+
return chat_history, chat_history
|
12 |
+
|
13 |
+
def translate_text(text):
|
14 |
+
response = requests.post(f"{API_URL}/translate", json={"text": text})
|
15 |
+
if response.status_code == 200:
|
16 |
+
return response.json()["translation"]
|
17 |
+
else:
|
18 |
+
return "Lỗi khi dịch văn bản"
|
19 |
+
|
20 |
+
def create_text_file(text, name, format):
|
21 |
+
response = requests.post(f"{API_URL}/create_file", json={"text": text, "filename": name, "file_format": format})
|
22 |
+
if response.status_code == 200:
|
23 |
+
with open(f"{name}.{format}", "wb") as f:
|
24 |
+
f.write(response.content)
|
25 |
+
return f"{name}.{format}"
|
26 |
+
else:
|
27 |
+
return "Lỗi khi tạo file"
|
28 |
+
|
29 |
+
def send_email(subject, body, to_email):
|
30 |
+
response = requests.post(f"{API_URL}/send_email", json={"subject": subject, "body": body, "to_email": to_email})
|
31 |
+
if response.status_code == 200:
|
32 |
+
return "Email đã được gửi thành công"
|
33 |
+
else:
|
34 |
+
return f"Lỗi khi gửi email: {response.json().get('message')}"
|
35 |
+
|
36 |
+
def chatbot_interface():
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
chatbot = gr.Chatbot()
|
39 |
+
msg = gr.Textbox(placeholder="Nhập tin nhắn...")
|
40 |
+
clear = gr.Button("Xóa hội thoại")
|
41 |
+
translate_input = gr.Textbox(placeholder="Nhập văn bản để dịch...")
|
42 |
+
translate_button = gr.Button("Dịch sang tiếng Việt")
|
43 |
+
translate_output = gr.Textbox(label="Bản dịch")
|
44 |
+
file_text = gr.Textbox(placeholder="Nhập văn bản để tạo file...")
|
45 |
+
file_name = gr.Textbox(placeholder="Nhập tên file (không bao gồm đuôi)...")
|
46 |
+
file_format = gr.Dropdown(choices=["docx", "pdf"], label="Chọn định dạng file")
|
47 |
+
create_file_button = gr.Button("Tạo File")
|
48 |
+
download = gr.File(label="Tải về file", interactive=False)
|
49 |
+
email_subject = gr.Textbox(placeholder="Nhập tiêu đề email...")
|
50 |
+
email_body = gr.Textbox(placeholder="Nhập nội dung email...", lines=5)
|
51 |
+
email_to = gr.Textbox(placeholder="Nhập địa chỉ email người nhận...")
|
52 |
+
send_email_button = gr.Button("Gửi Email")
|
53 |
+
|
54 |
+
def respond(message, chat_history):
|
55 |
+
response = get_text_response(message, chat_history)
|
56 |
+
chat_history.append(("Người dùng", message))
|
57 |
+
chat_history.append(("Chatbot", response))
|
58 |
+
return chat_history, chat_history
|
59 |
+
|
60 |
+
def clear_chat():
|
61 |
+
return [], []
|
62 |
+
|
63 |
+
def translate_and_display(text):
|
64 |
+
translation = translate_text(text)
|
65 |
+
return translation
|
66 |
+
|
67 |
+
def create_and_return_file(text, name, format):
|
68 |
+
file_path = create_text_file(text, name, format)
|
69 |
+
return file_path
|
70 |
+
|
71 |
+
def handle_send_email(subject, body, to_email):
|
72 |
+
email_response = send_email(subject, body, to_email)
|
73 |
+
return email_response
|
74 |
+
|
75 |
+
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
|
76 |
+
clear.click(clear_chat, [], [chatbot])
|
77 |
+
translate_button.click(translate_and_display, [translate_input], [translate_output])
|
78 |
+
create_file_button.click(create_and_return_file, [file_text, file_name, file_format], [download])
|
79 |
+
send_email_button.click(handle_send_email, [email_subject, email_body, email_to], [])
|
80 |
+
|
81 |
+
return demo
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo = chatbot_interface()
|
85 |
+
demo.launch()
|