roolaml commited on
Commit
ffd7569
·
1 Parent(s): fe70f7c

Add application file

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ from datetime import datetime
4
+ import pandas as pd
5
+
6
+
7
+ # Đặt API key của bạn ở đây
8
+ api_key = "sk-optimi-ai-zR7o9I8TWt6vOwOHJFEpT3BlbkFJlGLNXidFLiV3E10jQv1p"
9
+ openai.api_key = api_key
10
+
11
+ # Khởi tạo mô hình embedding
12
+
13
+
14
+ # Đọc dữ liệu từ JSONL
15
+ data_path = "/home/acer/Documents/ABI/Lich_optimi/nhansu_optimi.xlsx"
16
+ data = pd.read_excel(data_path)
17
+ data['index'] = range(len(data))
18
+
19
+ # Hàm truy vấn cơ sở dữ liệu để tìm tên đầy đủ của người tham gia
20
+ def get_full_name(participant):
21
+ list_name = []
22
+ for i in range(len(data)):
23
+ if participant in data['Họ và tên'][i]:
24
+ #print(data['Họ và tên'][i])
25
+ list_name.append(data['Họ và tên'][i])
26
+ return list_name
27
+ def replace_participants(reply, dict_replace):
28
+ for key, names in dict_replace.items():
29
+ if names:
30
+ # Join names with comma for display
31
+ names_str = ','.join(names)
32
+ # Replace the key with the list of names
33
+ reply = reply.replace(key, names_str)
34
+ else:
35
+ reply = reply.replace(key,'')
36
+ parts = reply.split('Người tham gia:')
37
+ if len(parts) > 1:
38
+ participants = parts[1].strip().split(',')
39
+ cleaned_participants = [p.strip() for p in participants if p.strip()]
40
+ reply = parts[0] + 'Người tham gia: ' + ', '.join(cleaned_participants)
41
+ return reply
42
+ # Định nghĩa hệ thống prompt
43
+ def create_system_prompt():
44
+ localtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
45
+ system_prompt = f"Tách yêu cầu sau thành 5 trường thông tin gồm: Tên cuộc họp (Ngắn gọn dưới 10 từ bao quát về mục đích cuộc họp), Ngày họp (DD/MM/YYYY), Giờ học bắt đầu (HH/MM), Giờ học kết thúc (HH/MM) và Người tham gia (Chỉ trích dẫn tên).\
46
+ Chú ý xác định thời điểm hiện tại là: {localtime} từ đó xác định ngày họp cụ thể bao gồm ngày, tháng, năm.\
47
+ Ví dụ: Tách yêu cầu 'họp với team AI, anh Hoa và giám đốc Dương design search ngày mai 10h sáng đến 3 giờ chiều.' thành:\
48
+ Tên cuộc họp: Design search.\
49
+ Ngày họp: 15/06/2024.\
50
+ Giờ bắt đầu: 10:00.\
51
+ Giờ kết thúc: 15:00.\
52
+ Người tham gia: Hoa,Dương.\
53
+ Bắt đầu tách yêu cầu sau: "
54
+ return system_prompt
55
+
56
+ # Hàm dự đoán cho Gradio
57
+ def predict(message, history):
58
+ # Tạo prompt hệ thống
59
+ system_prompt = create_system_prompt()
60
+
61
+ # Chuẩn bị lịch sử hội thoại
62
+ history_openai_format = [{"role": "system", "content": system_prompt}]
63
+ for human, assistant in history:
64
+ history_openai_format.append({"role": "user", "content": human})
65
+ history_openai_format.append({"role": "assistant", "content": assistant})
66
+ history_openai_format.append({"role": "user", "content": message})
67
+
68
+ # Gọi API của OpenAI
69
+ response = openai.ChatCompletion.create(
70
+ model='gpt-3.5-turbo',
71
+ messages=history_openai_format,
72
+ temperature=1.0
73
+ )
74
+
75
+ # Trích xuất phản hồi từ mô hình
76
+ reply = response.choices[0].message['content']
77
+ #print(reply)
78
+ #print(type(reply))
79
+ # Tách các tên người tham gia từ phản hồi
80
+ # Tách các tên người tham gia từ phản hồi
81
+ start_index = reply.find("Người tham gia:") + len("Người tham gia:")
82
+ end_index = reply.find("\n", start_index)
83
+
84
+ # Trích xuất danh sách người tham gia
85
+ participants_str = reply[start_index:end_index].strip()
86
+
87
+ # Tách danh sách thành từng phần tử
88
+ participants = [participant.strip() for participant in participants_str.split(",")]
89
+ participants = [participant.rstrip('.') for participant in participants_str.split(",")]
90
+ print(participants)
91
+ # Thay thế các tên người tham gia bằng tên đầy đủ
92
+ replace_dict = {participant: get_full_name(participant) for participant in participants}
93
+ print(replace_dict)
94
+ # replace_dict = {key: value for key, value in replace_dict.items() if value != []}
95
+ # print(replace_dict)
96
+ reply = replace_participants(reply,replace_dict)
97
+ print(reply)
98
+ # Cập nhật lịch sử hội thoại
99
+ history.append((message, reply))
100
+
101
+ return reply
102
+
103
+ # Khởi tạo giao diện Gradio
104
+ iface = gr.ChatInterface(fn=predict, title="Tách Thông Tin Cuộc Họp")
105
+
106
+ # Chạy Gradio
107
+ iface.launch(share=True)
108
+