Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sequence2txt_model.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from openai.lib.azure import AzureOpenAI
  17. from zhipuai import ZhipuAI
  18. import io
  19. from abc import ABC
  20. from ollama import Client
  21. from openai import OpenAI
  22. import os
  23. import json
  24. from rag.utils import num_tokens_from_string
  25. class Base(ABC):
  26. def __init__(self, key, model_name):
  27. pass
  28. def transcription(self, audio, **kwargs):
  29. transcription = self.client.audio.transcriptions.create(
  30. model=self.model_name,
  31. file=audio,
  32. response_format="text"
  33. )
  34. return transcription.text.strip(), num_tokens_from_string(transcription.text.strip())
  35. class GPTSeq2txt(Base):
  36. def __init__(self, key, model_name="whisper-1", base_url="https://api.openai.com/v1"):
  37. if not base_url: base_url = "https://api.openai.com/v1"
  38. self.client = OpenAI(api_key=key, base_url=base_url)
  39. self.model_name = model_name
  40. class QWenSeq2txt(Base):
  41. def __init__(self, key, model_name="paraformer-realtime-8k-v1", **kwargs):
  42. import dashscope
  43. dashscope.api_key = key
  44. self.model_name = model_name
  45. def transcription(self, audio, format):
  46. from http import HTTPStatus
  47. from dashscope.audio.asr import Recognition
  48. recognition = Recognition(model=self.model_name,
  49. format=format,
  50. sample_rate=16000,
  51. callback=None)
  52. result = recognition.call(audio)
  53. ans = ""
  54. if result.status_code == HTTPStatus.OK:
  55. for sentence in result.get_sentence():
  56. ans += str(sentence + '\n')
  57. return ans, num_tokens_from_string(ans)
  58. return "**ERROR**: " + result.message, 0
  59. class OllamaSeq2txt(Base):
  60. def __init__(self, key, model_name, lang="Chinese", **kwargs):
  61. self.client = Client(host=kwargs["base_url"])
  62. self.model_name = model_name
  63. self.lang = lang
  64. class AzureSeq2txt(Base):
  65. def __init__(self, key, model_name, lang="Chinese", **kwargs):
  66. self.client = AzureOpenAI(api_key=key, azure_endpoint=kwargs["base_url"], api_version="2024-02-01")
  67. self.model_name = model_name
  68. self.lang = lang
  69. class XinferenceSeq2txt(Base):
  70. def __init__(self, key, model_name="", base_url=""):
  71. self.client = OpenAI(api_key="xxx", base_url=base_url)
  72. self.model_name = model_name