Mr-Bhaskar commited on
Commit
5e0949f
1 Parent(s): 73a4723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -57
app.py CHANGED
@@ -1,63 +1,156 @@
1
  import gradio as gr
 
 
 
 
2
  from huggingface_hub import InferenceClient
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import tempfile
5
+ import os
6
  from huggingface_hub import InferenceClient
7
+ import re
8
+ from streaming_stt_nemo import Model
9
+ import torch
10
+ import random
11
 
12
+ default_lang = "en"
13
+
14
+ engines = { default_lang: Model(default_lang) }
15
+
16
+ def transcribe(audio):
17
+ lang = "en"
18
+ model = engines[lang]
19
+ text = model.stt_file(audio)[0]
20
+ return text
21
+
22
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
23
+
24
+ def client_fn(model):
25
+ if "Mixtral" in model:
26
+ return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
27
+ elif "Mr-Bhaskar/FusionBot" in model:
28
+ return InferenceClient("Mr-Bhaskar/FusionBot")
29
+ elif "fbt-llama2-7b" in model:
30
+ return InferenceClient("Mr-Bhaskar/fbt-llama2-7b")
31
+ elif "Mr-Bhaskar/FBt" in model:
32
+ return InferenceClient("Mr-Bhaskar/FBt")
33
+ elif "fbt-mistral7b-instruct" in model:
34
+ return InferenceClient("Mr-Bhaskar/fbt-mistral7b-instruct")
35
+ elif "fbt-mistral-7b" in model:
36
+ return InferenceClient("Mr-Bhaskar/fbt-mistral-7b")
37
+ elif "fbt-llama3-8b" in model:
38
+ return InferenceClient("Mr-Bhaskar/fbt-llama3-8b")
39
+ elif "fbt-gemma-7b" in model:
40
+ return InferenceClient("Mr-Bhaskar/fbt-gemma-7b")
41
+ elif "llama-8b-inst" in model:
42
+ return InferenceClient("Mr-Bhaskar/fbt-llama-8b-inst")
43
+ elif "gemma-7b-inst" in model:
44
+ return InferenceClient("Mr-Bhaskar/fbt-gemma-7b-inst")
45
+ elif "Llama" in model:
46
+ return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
47
+ elif "Mistral" in model:
48
+ return InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
49
+ elif "Phi" in model:
50
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
51
+ else:
52
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
53
+
54
+
55
+ def randomize_seed_fn(seed: int) -> int:
56
+ seed = random.randint(0, 999999)
57
+ return seed
58
+
59
+ system_instructions1 = "[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, Only answer the question asked by user, Do not say unnecessary things.[USER]"
 
 
 
 
 
 
 
 
60
 
61
+ def models(text, model="Mixtral 8x7B", seed=42):
62
+
63
+ seed = int(randomize_seed_fn(seed))
64
+ generator = torch.Generator().manual_seed(seed)
65
+
66
+ client = client_fn(model)
67
+
68
+ generate_kwargs = dict(
69
+ max_new_tokens=300,
70
+ seed=seed
71
+ )
72
+
73
+ formatted_prompt = system_instructions1 + text + "[JARVIS]"
74
+ stream = client.text_generation(
75
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
76
+ output = ""
77
+ for response in stream:
78
+ if not response.token.text == "</s>":
79
+ output += response.token.text
80
+
81
+ return output
82
+
83
+ async def respond(audio, model, seed):
84
+ user = transcribe(audio)
85
+ reply = models(user, model, seed)
86
+ communicate = edge_tts.Communicate(reply)
87
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
88
+ tmp_path = tmp_file.name
89
+ await communicate.save(tmp_path)
90
+ yield tmp_path
91
+
92
+ DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
93
+ ### <center>A personal Assistant of Tony Stark for YOU
94
+ ### <center>Voice Chat with your personal Assistant</center>
95
+ """
96
+
97
+ with gr.Blocks(css="style.css") as demo:
98
+ gr.Markdown(DESCRIPTION)
99
+ with gr.Row():
100
+ select = gr.Dropdown([ 'Mixtral 8x7B',
101
+ 'Llama 3 8B',
102
+ 'Mistral 7B v0.3',
103
+ 'Phi 3 mini',
104
+ ],
105
+ value="Mistral 7B v0.3",
106
+ label="Model"
107
+ )
108
+ seed = gr.Slider(
109
+ label="Seed",
110
+ minimum=0,
111
+ maximum=999999,
112
+ step=1,
113
+ value=0,
114
+ visible=False
115
+ )
116
+ input = gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False)
117
+ output = gr.Audio(label="AI", type="filepath",
118
+ interactive=False,
119
+ autoplay=True,
120
+ elem_classes="audio")
121
+ gr.Interface(
122
+ batch=True,
123
+ max_batch_size=10,
124
+ fn=respond,
125
+ inputs=[input, select, seed],
126
+ outputs=[output], live=True)
127
+
128
+ with gr.Row():
129
+ select = gr.Dropdown([ 'fbt-mistral-7b',
130
+ 'Mixtral 8x7B',
131
+ 'Llama 3 8B',
132
+ 'Mistral 7B v0.3',
133
+ 'Phi 3 mini',
134
+ ],
135
+ value="Mistral 7B v0.3",
136
+ label="Model"
137
+ )
138
+ seed = gr.Slider(
139
+ label="Seed",
140
+ minimum=0,
141
+ maximum=999999,
142
+ step=1,
143
+ value=0,
144
+ visible=False
145
+ )
146
+ input = gr.Textbox(label="User")
147
+ output = gr.Textbox(label="AI", interactive=False)
148
+ gr.Interface(
149
+ batch=True,
150
+ max_batch_size=10,
151
+ fn=models,
152
+ inputs=[input, select, seed],
153
+ outputs=[output], live=True)
154
 
155
  if __name__ == "__main__":
156
+ demo.queue(max_size=200).launch()