woonchen commited on
Commit
4fcb5d4
·
verified ·
1 Parent(s): 5b7daaf

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -1
main.py CHANGED
@@ -8,6 +8,7 @@ from langchain.chains import ConversationalRetrievalChain
8
  import pdfplumber
9
  import os
10
  import google.generativeai as genai
 
11
 
12
  # 初始化 FastAPI 應用
13
  app = FastAPI()
@@ -58,6 +59,18 @@ if docs:
58
  else:
59
  raise ValueError("No documents found in the 'data' directory.")
60
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # 初始化 chat_history 作為全局變數
62
  chat_history = []
63
 
@@ -71,8 +84,11 @@ def invoke(question: str):
71
  )
72
 
73
  # 呼叫 QA chain 並處理回應
 
74
  response = qa_chain.invoke({"question": question, "chat_history": chat_history})
75
- return response['answer']
 
 
76
 
77
  except Exception as e:
78
  print(f"Error during invoke: {e}")
 
8
  import pdfplumber
9
  import os
10
  import google.generativeai as genai
11
+ from deep_translator import GoogleTranslator
12
 
13
  # 初始化 FastAPI 應用
14
  app = FastAPI()
 
59
  else:
60
  raise ValueError("No documents found in the 'data' directory.")
61
 
62
+
63
+ # 定義翻譯函數
64
+ def translate_to_english(text):
65
+ return GoogleTranslator(source='auto', target='en').translate(text)
66
+
67
+ def translate_to_chinese(text):
68
+ return GoogleTranslator(source='auto', target='zh-TW').translate(text)
69
+
70
+
71
+
72
+
73
+
74
  # 初始化 chat_history 作為全局變數
75
  chat_history = []
76
 
 
84
  )
85
 
86
  # 呼叫 QA chain 並處理回應
87
+ question = translate_to_english(question)
88
  response = qa_chain.invoke({"question": question, "chat_history": chat_history})
89
+ response = translate_to_chinese(response['answer'])
90
+ # return response['answer']
91
+ return response
92
 
93
  except Exception as e:
94
  print(f"Error during invoke: {e}")