How to fix `transformers` package not found error in a Python project with `py-langchain`, `llama-index`, and `gradio`?

huangapple go评论110阅读模式
英文:

How to fix `transformers` package not found error in a Python project with `py-langchain`, `llama-index`, and `gradio`?

问题

I get this error ModuleNotFoundError: No module named 'transformers' even though I did the pip install transformers command. Could you kindly help me? Thank you

我的代码:

  1. import os
  2. import sys
  3. from dotenv import load_dotenv
  4. import gradio as gr
  5. from langchain import OpenAI
  6. from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper
  7. load_dotenv()
  8. os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
  9. def construct_index(directory_path):
  10. max_input_size = 4096
  11. num_outputs = 512
  12. max_chunk_overlap = 20
  13. chunk_size_limit = 600
  14. prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
  15. llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))
  16. documents = SimpleDirectoryReader(directory_path).load_data()
  17. index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
  18. index.save_to_disk('index.json')
  19. return index
  20. def qabot(input_text):
  21. index = GPTVectorStoreIndex.load_from_disk('index.json')
  22. response = index.query(input_text, response_mode="compact")
  23. return response.response
  24. iface = gr.Interface(fn=qabot, inputs=gr.inputs.Textbox(lines=7, label='Enter you query'), outputs="text", title="Custom-trained QA Application")
  25. index = construct_index("docs")
  26. iface.launch(share=True)

注意:我已经删除了代码中的 HTML 实体编码(例如,& 变成了 &),以便更好地呈现代码。

英文:

I get this error ModuleNotFoundError: No module named 'transformers' even though I did the pip install transformers command. Could you kindly help me? Thank you

My code:

  1. import os
  2. import sys
  3. from dotenv import load_dotenv
  4. import gradio as gr
  5. from langchain import OpenAI
  6. from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper
  7. ##from transformers import pipeline
  8. load_dotenv()
  9. os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
  10. def construct_index(directory_path):
  11. max_input_size = 4096
  12. num_outputs = 512
  13. max_chunk_overlap = 20
  14. chunk_size_limit = 600
  15. prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
  16. llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))
  17. documents = SimpleDirectoryReader(directory_path).load_data()
  18. index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
  19. index.save_to_disk('index.json')
  20. return index
  21. def qabot(input_text):
  22. index = GPTVectorStoreIndex.load_from_disk('index.json')
  23. response = index.query(input_text, response_mode="compact")
  24. return response.response
  25. iface = gr.Interface(fn=qabot, inputs=gr.inputs.Textbox(lines=7, label='Enter you query'), outputs="text", title="Custom-trained QA Application")
  26. index = construct_index("docs")
  27. iface.launch(share=True)

答案1

得分: 1

也许你可以取消注释

  1. from transformers import pipeline
英文:

Maybe you can uncomment

  1. ##from transformers import pipeline

huangapple
  • 本文由 发表于 2023年6月1日 08:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定