import gradio as gr import spaces import soundfile as sf import torch from datetime import datetime import random import time from datetime import datetime import whisper import torch from transformers import AutoModelForCausalLM, AutoTokenizer, VitsModel import torch import numpy as np import os from timeit import default_timer as timer import torch import numpy as np import pandas as pd import whisper DESCRIPTION = """\ # Ai Trek - Generative AI usage This Space demonstrates LAIONBOT functionalities, 🔎 Large Language Models is a model notable for its ability to achieve general-purpose language generation and understanding. 🔨 On this demo, we can play with it not only by using text, but also asking questions and getting answers by Text to speech model. """ def load_whisper(): return whisper.load_model("medium", device = 'cpu') def load_tts(): tts_model = VitsModel.from_pretrained("facebook/mms-tts-pol") #tts_model.to("cuda") tokenizer_tss = AutoTokenizer.from_pretrained("facebook/mms-tts-pol") return tts_model, tokenizer_tss def save_to_txt(text_to_save): with open('prompt.txt', 'w', encoding='utf-8') as f: f.write(text_to_save) def read_txt(): with open('prompt.txt') as f: lines = f.readlines() return lines def _load_model_tokenizer(): model_id = 'tangger/Qwen-7B-Chat' tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",trust_remote_code=True, fp16=True).eval() return model, tokenizer whisper_model = load_whisper() if torch.cuda.is_available(): whisper_model = whisper_model.to(device='cuda') #whisper_model = load_whisper() tts_model, tokenizer_tss = load_tts() model, tokenizer = _load_model_tokenizer() def postprocess(self, y): if y is None: return [] for i, (message, response) in enumerate(y): y[i] = ( None if message is None else mdtex2html.convert(message), None if response is None else mdtex2html.convert(response), ) return y def _parse_text(text): lines = text.split("\n") lines = [line for line in lines if line != ""] count = 0 for i, line in enumerate(lines): if "```" in line: count += 1 items = line.split("`") if count % 2 == 1: lines[i] = f'
'
else:
lines[i] = f"
"
else:
if i > 0:
if count % 2 == 1:
line = line.replace("`", r"\`")
line = line.replace("<", "<")
line = line.replace(">", ">")
line = line.replace(" ", " ")
line = line.replace("*", "*")
line = line.replace("_", "_")
line = line.replace("-", "-")
line = line.replace(".", ".")
line = line.replace("!", "!")
line = line.replace("(", "(")
line = line.replace(")", ")")
line = line.replace("$", "$")
lines[i] = "