Aspik101 commited on
Commit
dc45eb5
1 Parent(s): 0fbbaea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1,14 +1,30 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM, AutoTokenizer
2
  import soundfile as sf
3
  import torch
4
-
5
  from datetime import datetime
6
  import random
7
  import time
 
 
 
 
 
8
  import numpy as np
9
  import os
10
  import argparse
11
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  def save_to_txt(text_to_save):
@@ -21,7 +37,6 @@ def read_txt():
21
  return lines
22
 
23
 
24
-
25
  ##### Chat z LLAMA ####
26
  ##### Chat z LLAMA ####
27
  ##### Chat z LLAMA ####
@@ -32,7 +47,7 @@ def _load_model_tokenizer():
32
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
33
  model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",trust_remote_code=True, fp16=True).eval()
34
  return model, tokenizer
35
- model_llm, tokenizer_llm = _load_model_tokenizer()
36
 
37
 
38
  def postprocess(self, y):
@@ -82,7 +97,7 @@ def predict(_query, _chatbot, _task_history):
82
  _chatbot.append((_parse_text(_query), ""))
83
  full_response = ""
84
 
85
- for response in model_llm.chat_stream(tokenizer_llm, _query, history=_task_history,system = "Jesteś assystentem AI. Odpowiadaj zawsze w języku poslkim" ):
86
  _chatbot[-1] = (_parse_text(_query), _parse_text(response))
87
 
88
  yield _chatbot
@@ -106,8 +121,9 @@ def update_audio(text):
106
 
107
  def translate(audio):
108
  print("__Wysyłam nagranie do whisper!")
109
- transcription = whisper_model.transcribe(audio, language="pl")
110
- return transcription["text"]
 
111
 
112
 
113
  def predict(audio, _chatbot, _task_history):
@@ -118,7 +134,7 @@ def predict(audio, _chatbot, _task_history):
118
  _chatbot.append((_parse_text(_query), ""))
119
  full_response = ""
120
 
121
- for response in model_llm.chat_stream(tokenizer_llm,
122
  _query,
123
  history= _task_history,
124
  system = "Jesteś assystentem AI. Odpowiadaj zawsze w języku polskim. Odpowiadaj krótko."):
@@ -132,7 +148,6 @@ def predict(audio, _chatbot, _task_history):
132
  print("____full_response",full_response)
133
  audio_file = read_text(_parse_text(full_response)) # Generowanie audio
134
  return full_response
135
- # return 'temp_file.wav' # Zwrócenie ścieżki do pliku audio
136
 
137
  def regenerate(_chatbot, _task_history):
138
  if not _task_history:
@@ -158,18 +173,6 @@ with gr.Blocks() as chat_demo:
158
  submit_btn.click(predict, [query, chatbot, task_history], [chatbot], show_progress=True)
159
  submit_audio_btn.click(predict, [audio_upload, chatbot, task_history], [chatbot], show_progress=True).then(update_audio, chatbot, audio_output)
160
 
 
161
 
162
 
163
-
164
-
165
- ##### Audio Gen ####
166
- ##### Audio Gen ####
167
- ##### Audio Gen ####
168
-
169
-
170
- ##### Run Alll #######
171
- ##### Run Alll #######
172
- ##### Run Alll #######
173
-
174
- chat_demo.queue()
175
- chat_demo.launch()
 
1
+
2
  import soundfile as sf
3
  import torch
 
4
  from datetime import datetime
5
  import random
6
  import time
7
+ from datetime import datetime
8
+ import whisper
9
+ import torch
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer, VitsModel
11
+ import torch
12
  import numpy as np
13
  import os
14
  import argparse
15
  import gradio as gr
16
+ from timeit import default_timer as timer
17
+ import torch
18
+ import numpy as np
19
+ import pandas as pd
20
+ import whisper
21
+
22
+
23
+ # whisper_model = whisper.load_model("medium").to("cuda")
24
+ tts_model = VitsModel.from_pretrained("facebook/mms-tts-pol")
25
+ tts_model.to("cuda")
26
+ print("TTS Loaded!")
27
+ tokenizer_tss = AutoTokenizer.from_pretrained("facebook/mms-tts-pol")
28
 
29
 
30
  def save_to_txt(text_to_save):
 
37
  return lines
38
 
39
 
 
40
  ##### Chat z LLAMA ####
41
  ##### Chat z LLAMA ####
42
  ##### Chat z LLAMA ####
 
47
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
48
  model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",trust_remote_code=True, fp16=True).eval()
49
  return model, tokenizer
50
+
51
 
52
 
53
  def postprocess(self, y):
 
97
  _chatbot.append((_parse_text(_query), ""))
98
  full_response = ""
99
 
100
+ for response in model.chat_stream(tokenizer, _query, history=_task_history,system = "Jesteś assystentem AI. Odpowiadaj zawsze w języku poslkim" ):
101
  _chatbot[-1] = (_parse_text(_query), _parse_text(response))
102
 
103
  yield _chatbot
 
121
 
122
  def translate(audio):
123
  print("__Wysyłam nagranie do whisper!")
124
+ # transcription = whisper_model.transcribe(audio, language="pl")
125
+ return "Co możesz powiedzieć o ING Banku Śląskim?"
126
+ # return transcription["text"]
127
 
128
 
129
  def predict(audio, _chatbot, _task_history):
 
134
  _chatbot.append((_parse_text(_query), ""))
135
  full_response = ""
136
 
137
+ for response in model.chat_stream(tokenizer,
138
  _query,
139
  history= _task_history,
140
  system = "Jesteś assystentem AI. Odpowiadaj zawsze w języku polskim. Odpowiadaj krótko."):
 
148
  print("____full_response",full_response)
149
  audio_file = read_text(_parse_text(full_response)) # Generowanie audio
150
  return full_response
 
151
 
152
  def regenerate(_chatbot, _task_history):
153
  if not _task_history:
 
173
  submit_btn.click(predict, [query, chatbot, task_history], [chatbot], show_progress=True)
174
  submit_audio_btn.click(predict, [audio_upload, chatbot, task_history], [chatbot], show_progress=True).then(update_audio, chatbot, audio_output)
175
 
176
+ chat_demo.queue().launch(share=True)
177
 
178