Как вернуть ответ при работе с несколькими потоками в Python?Python

Программы на Python
Anonymous
Как вернуть ответ при работе с несколькими потоками в Python?

Сообщение Anonymous »

Как мне получить ответ при звонке

Код: Выделить всё

response = gemini_pdf(message)
Я хочу, чтобы ответ содержал конечный результат. Я застрял здесь.
Вот мой код:

Код: Выделить всё

def read_pdf(file_path):
global file_content
global file_name

# Read the contents of the PDF file
pdfreader = PdfReader(file_path)
from typing_extensions import Concatenate
# read text from pdf
raw_text = ''
try:
for i, page in enumerate(pdfreader.pages):
content = page.extract_text()
if content:
raw_text += content

print(raw_text)
text_splitter = CharacterTextSplitter(
separator = "\n",
chunk_size = 800,
chunk_overlap  = 200,
length_function = len,
)
new_content = text_splitter.split_text(raw_text)
len(new_content)
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
file_content = FAISS.from_texts(new_content, embeddings)

except TypeError as e:

print("Sorry, an error occurred while processing your request.", f"{e}")
pass
except FileNotFoundError as e:

print("Sorry, an error occurred while processing your request.", f"{e}")
pass
except Exception as e:
print("Sorry, an error occurred while processing your request.", f"{e}")
tk.messagebox.showwarning("An error occurred", f"{e}")

return

def answer_ftom_pdf(query):
global file_content

model = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest",
temperature=0.3)

#prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
docs = file_content.similarity_search(query)
chain = load_qa_chain(model, chain_type="stuff")
if file_content != None:
response = chain.run(input_documents=docs, question=query)
print(response)
response = response.strip()
print("Here")
return response
if file_content  == None:
print("Content is none")

def gemini_pdf(query=None):
global file_content
global file_name

if file_content is None:
# Open a file dialog to select the PDF file
file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
file_name = os.path.basename(file_path)
if file_name:
threading.Thread(target=read_pdf, args=(file_path,)).start()

elif file_content:

print("Same PDF")
threading.Thread(target=answer_ftom_pdf, args=(query,)).start()

Как обеспечить, чтобы ответ содержал конечный результат при вызове Gemini_pdf(message)

Подробнее здесь: https://stackoverflow.com/questions/787 ... -in-python

Вернуться в «Python»