TDN-M commited on
Commit
03cdcac
·
verified ·
1 Parent(s): 98ce3ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -4
app.py CHANGED
@@ -3,7 +3,11 @@ import gradio as gr
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
 
 
 
6
 
 
7
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
8
 
9
  template = """You are a very helpful assistant in providing users with information and knowledge about statistics. Additionally, you are also an expert in translating specialized documents on statistics, artificial intelligence, and technology from foreign languages into Vietnamese in a complete, coherent manner while ensuring the original knowledge from the foreign documents is accurately conveyed.
@@ -18,17 +22,80 @@ prompt = PromptTemplate(
18
  memory = ConversationBufferMemory(memory_key="chat_history")
19
 
20
  llm_chain = LLMChain(
21
- llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo"),
22
  prompt=prompt,
23
  verbose=True,
24
  memory=memory,
25
  )
26
 
27
- def get_text_response(user_message, history):
28
  response = llm_chain.predict(user_message=user_message)
29
  return response
30
 
31
- demo = gr.ChatInterface(get_text_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
- demo.launch() # To create a public link, set `share=True` in `launch()`. To enable errors and logs
 
 
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
+ import openai
7
+ from docx import Document
8
+ from fpdf import FPDF
9
 
10
+ # Lấy API key từ biến môi trường
11
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
12
 
13
  template = """You are a very helpful assistant in providing users with information and knowledge about statistics. Additionally, you are also an expert in translating specialized documents on statistics, artificial intelligence, and technology from foreign languages into Vietnamese in a complete, coherent manner while ensuring the original knowledge from the foreign documents is accurately conveyed.
 
22
  memory = ConversationBufferMemory(memory_key="chat_history")
23
 
24
  llm_chain = LLMChain(
25
+ llm=ChatOpenAI(temperature=0.5, model_name="gpt-4"),
26
  prompt=prompt,
27
  verbose=True,
28
  memory=memory,
29
  )
30
 
31
+ def get_text_response(user_message, chat_history):
32
  response = llm_chain.predict(user_message=user_message)
33
  return response
34
 
35
+ def translate_text(text):
36
+ prompt = f"Translate the following text to Vietnamese: {text}"
37
+ response = openai.Completion.create(
38
+ engine="gpt-4o-mini",
39
+ prompt=prompt,
40
+ max_tokens=150
41
+ )
42
+ translation = response.choices[0].text.strip()
43
+ return translation
44
+
45
+ def create_text_file(text, filename, file_format):
46
+ if file_format == 'docx':
47
+ doc = Document()
48
+ doc.add_paragraph(text)
49
+ doc_path = f"{filename}.docx"
50
+ doc.save(doc_path)
51
+ return doc_path
52
+ elif file_format == 'pdf':
53
+ pdf = FPDF()
54
+ pdf.add_page()
55
+ pdf.set_font("Arial", size=12)
56
+ pdf.multi_cell(0, 10, text)
57
+ pdf_path = f"{filename}.pdf"
58
+ pdf.output(pdf_path)
59
+ return pdf_path
60
+
61
+ def chatbot_interface():
62
+ with gr.Blocks() as demo:
63
+ chatbot = gr.Chatbot()
64
+ msg = gr.Textbox(placeholder="Nhập tin nhắn...")
65
+ clear = gr.Button("Xóa hội thoại")
66
+ translate_input = gr.Textbox(placeholder="Nhập văn bản để dịch...")
67
+ translate_button = gr.Button("Dịch sang tiếng Việt")
68
+ translate_output = gr.Textbox(label="Bản dịch")
69
+ file_text = gr.Textbox(placeholder="Nhập văn bản để tạo file...")
70
+ file_name = gr.Textbox(placeholder="Nhập tên file (không bao gồm đuôi)...")
71
+ file_format = gr.Dropdown(choices=["docx", "pdf"], label="Chọn định dạng file")
72
+ create_file_button = gr.Button("Tạo File")
73
+ download = gr.File(label="Tải về file", interactive=False)
74
+
75
+ def respond(message, chat_history):
76
+ response = get_text_response(message, chat_history)
77
+ chat_history.append(("Người dùng", message))
78
+ chat_history.append(("Chatbot", response))
79
+ return chat_history, chat_history
80
+
81
+ def clear_chat():
82
+ return [], []
83
+
84
+ def translate_and_display(text):
85
+ translation = translate_text(text)
86
+ return translation
87
+
88
+ def create_and_return_file(text, name, format):
89
+ file_path = create_text_file(text, name, format)
90
+ return file_path
91
+
92
+ msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
93
+ clear.click(clear_chat, [], [chatbot])
94
+ translate_button.click(translate_and_display, [translate_input], [translate_output])
95
+ create_file_button.click(create_and_return_file, [file_text, file_name, file_format], [download])
96
+
97
+ return demo
98
 
99
  if __name__ == "__main__":
100
+ demo = chatbot_interface()
101
+ demo.launch()