KingNish commited on
Commit
679f566
1 Parent(s): eae3b08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -7
app.py CHANGED
@@ -21,22 +21,44 @@ def transcribe(audio):
21
  text = model.stt_file(audio)[0]
22
  return text
23
 
24
- client1 = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  system_instructions1 = "<s>[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'Tony Stark.' The expectation is that I will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by the user and not an AI-powered assistant. [USER]"
27
 
28
- def model(text):
 
 
 
 
 
 
29
  generate_kwargs = dict(
30
  temperature=0.7,
31
  max_new_tokens=512,
32
  top_p=0.95,
33
  repetition_penalty=1,
34
  do_sample=True,
35
- seed=42,
36
  )
37
 
38
  formatted_prompt = system_instructions1 + text + "[JARVIS]"
39
- stream = client1.text_generation(
40
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
41
  output = ""
42
  for response in stream:
@@ -47,7 +69,7 @@ def model(text):
47
 
48
  async def respond(audio):
49
  user = transcribe(audio)
50
- reply = model(user)
51
  communicate = edge_tts.Communicate(reply)
52
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
53
  tmp_path = tmp_file.name
@@ -62,6 +84,22 @@ DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
62
  with gr.Blocks(css="style.css") as demo:
63
  gr.Markdown(DESCRIPTION)
64
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  input = gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False)
66
  output = gr.Audio(label="AI", type="filepath",
67
  interactive=False,
@@ -71,8 +109,8 @@ with gr.Blocks(css="style.css") as demo:
71
  batch=True,
72
  max_batch_size=10,
73
  fn=respond,
74
- inputs=[input],
75
- outputs=[output], live=False)
76
 
77
  if __name__ == "__main__":
78
  demo.queue(max_size=200).launch()
 
21
  text = model.stt_file(audio)[0]
22
  return text
23
 
24
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
25
+
26
+ def client_fn(model):
27
+ if "Mixtral" in model:
28
+ return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
29
+ elif "Llama" in model:
30
+ return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
31
+ elif "Mistral" in model:
32
+ return InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
33
+ elif "Phi" in model:
34
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
35
+ else:
36
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
37
+
38
+ def randomize_seed_fn(seed: int) -> int:
39
+ seed = random.randint(0, 999999)
40
+ return seed
41
 
42
  system_instructions1 = "<s>[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'Tony Stark.' The expectation is that I will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by the user and not an AI-powered assistant. [USER]"
43
 
44
+ def models(text, model="Mixtral 8x7B", seed=42):
45
+
46
+ seed = int(randomize_seed_fn(seed))
47
+ generator = torch.Generator().manual_seed(seed)
48
+
49
+ client = client_fn(model)
50
+
51
  generate_kwargs = dict(
52
  temperature=0.7,
53
  max_new_tokens=512,
54
  top_p=0.95,
55
  repetition_penalty=1,
56
  do_sample=True,
57
+ seed=seed,
58
  )
59
 
60
  formatted_prompt = system_instructions1 + text + "[JARVIS]"
61
+ stream = client.text_generation(
62
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
63
  output = ""
64
  for response in stream:
 
69
 
70
  async def respond(audio):
71
  user = transcribe(audio)
72
+ reply = models(user, model, seed)
73
  communicate = edge_tts.Communicate(reply)
74
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
75
  tmp_path = tmp_file.name
 
84
  with gr.Blocks(css="style.css") as demo:
85
  gr.Markdown(DESCRIPTION)
86
  with gr.Row():
87
+ select = gr.Dropdown([ 'Mixtral 8x7B',
88
+ 'Llama 3 8B',
89
+ 'Mistral 7B v0.3',
90
+ 'Phi 3 mini',
91
+ ],
92
+ value="Mistral 7B v0.3",
93
+ label="Model"
94
+ )
95
+ seed = gr.Slider(
96
+ label="Seed",
97
+ minimum=0,
98
+ maximum=999999,
99
+ step=1,
100
+ value=0,
101
+ visible=False
102
+ )
103
  input = gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False)
104
  output = gr.Audio(label="AI", type="filepath",
105
  interactive=False,
 
109
  batch=True,
110
  max_batch_size=10,
111
  fn=respond,
112
+ inputs=[input, select, seed],
113
+ outputs=[output], live=True)
114
 
115
  if __name__ == "__main__":
116
  demo.queue(max_size=200).launch()