Spaces:
Sleeping
Sleeping
Ankit Yadav
commited on
Commit
Β·
30f323a
1
Parent(s):
6f50841
Jarvis Model
Browse files
README.md
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: true
|
10 |
-
|
11 |
-
short_description: Latest text-generation model by META - Meta Llama3 8b.
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: JARVIS
|
3 |
+
emoji: π₯
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.28.3
|
8 |
app_file: app.py
|
9 |
pinned: true
|
10 |
+
short_description: Voice Assistant like JARVIS
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,146 +1,119 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
<
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
---
|
25 |
-
Built with Meta Llama 3
|
26 |
-
"""
|
27 |
-
|
28 |
-
PLACEHOLDER = """
|
29 |
-
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
30 |
-
<img src="https://ysharma-dummy-chat-app.hf.space/file=/tmp/gradio/8e75e61cc9bab22b7ce3dec85ab0e6db1da5d107/Meta_lockup_positive%20primary_RGB.jpg" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
|
31 |
-
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
|
32 |
-
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
33 |
-
</div>
|
34 |
-
"""
|
35 |
-
|
36 |
-
|
37 |
-
css = """
|
38 |
-
h1 {
|
39 |
-
text-align: center;
|
40 |
-
display: block;
|
41 |
-
}
|
42 |
-
|
43 |
-
#duplicate-button {
|
44 |
-
margin: auto;
|
45 |
-
color: white;
|
46 |
-
background: #1565c0;
|
47 |
-
border-radius: 100vh;
|
48 |
-
}
|
49 |
-
"""
|
50 |
-
|
51 |
-
# Load the tokenizer and model
|
52 |
-
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
53 |
-
model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1", device_map="auto") # to("cuda:0")
|
54 |
-
terminators = [
|
55 |
-
tokenizer.eos_token_id,
|
56 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
57 |
-
]
|
58 |
-
|
59 |
-
@spaces.GPU(duration=120)
|
60 |
-
def chat_llama3_8b(message: str,
|
61 |
-
history: list,
|
62 |
-
temperature: float,
|
63 |
-
max_new_tokens: int
|
64 |
-
) -> str:
|
65 |
-
"""
|
66 |
-
Generate a streaming response using the llama3-8b model.
|
67 |
-
Args:
|
68 |
-
message (str): The input message.
|
69 |
-
history (list): The conversation history used by ChatInterface.
|
70 |
-
temperature (float): The temperature for generating the response.
|
71 |
-
max_new_tokens (int): The maximum number of new tokens to generate.
|
72 |
-
Returns:
|
73 |
-
str: The generated response.
|
74 |
-
"""
|
75 |
-
conversation = []
|
76 |
-
for user, assistant in history:
|
77 |
-
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
78 |
-
conversation.append({"role": "user", "content": message})
|
79 |
-
|
80 |
-
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
81 |
-
|
82 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
generate_kwargs = dict(
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
88 |
do_sample=True,
|
89 |
-
|
90 |
-
eos_token_id=terminators,
|
91 |
)
|
92 |
-
|
93 |
-
|
94 |
-
generate_kwargs
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
|
106 |
-
|
107 |
-
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
gr.Markdown(DESCRIPTION)
|
112 |
-
gr.
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
maximum=4096,
|
127 |
-
step=1,
|
128 |
-
value=512,
|
129 |
-
label="Max new tokens",
|
130 |
-
render=False ),
|
131 |
-
],
|
132 |
-
examples=[
|
133 |
-
['How to setup a human base on Mars? Give short answer.'],
|
134 |
-
['Explain theory of relativity to me like Iβm 8 years old.'],
|
135 |
-
['What is 9,000 * 9,000?'],
|
136 |
-
['Write a pun-filled happy birthday message to my friend Alex.'],
|
137 |
-
['Justify why a penguin might make a good king of the jungle.']
|
138 |
-
],
|
139 |
-
cache_examples=False,
|
140 |
-
)
|
141 |
-
|
142 |
-
gr.Markdown(LICENSE)
|
143 |
-
|
144 |
if __name__ == "__main__":
|
145 |
-
demo.launch()
|
146 |
-
|
|
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
+
import gradio as gr
|
4 |
+
import edge_tts
|
5 |
+
import asyncio
|
6 |
+
import time
|
7 |
+
import tempfile
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
+
|
10 |
+
DESCRIPTION = """ # <center><b>JARVISβ‘</b></center>
|
11 |
+
### <center>A personal Assistant of Tony Stark for YOU
|
12 |
+
### <center>Currently It supports text input, But If this space completes 1k hearts than I starts working on Audio Input.</center>
|
13 |
+
"""
|
14 |
+
|
15 |
+
MORE = """ ## TRY Other Models
|
16 |
+
### Instant Video: Create Amazing Videos in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Video
|
17 |
+
### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
|
18 |
+
"""
|
19 |
+
|
20 |
+
Fast = """## Fastest Model"""
|
21 |
+
|
22 |
+
Complex = """## Best in Complex Question"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
Detail = """## Best for Detailed Generation or Long Answers"""
|
25 |
+
|
26 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
27 |
+
|
28 |
+
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]"
|
29 |
+
|
30 |
+
async def generate1(prompt):
|
31 |
generate_kwargs = dict(
|
32 |
+
temperature=0.6,
|
33 |
+
max_new_tokens=256,
|
34 |
+
top_p=0.95,
|
35 |
+
repetition_penalty=1,
|
36 |
do_sample=True,
|
37 |
+
seed=42,
|
|
|
38 |
)
|
39 |
+
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
40 |
+
stream = client1.text_generation(
|
41 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
42 |
+
output = ""
|
43 |
+
for response in stream:
|
44 |
+
output += response.token.text
|
45 |
+
|
46 |
+
communicate = edge_tts.Communicate(output)
|
47 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
48 |
+
tmp_path = tmp_file.name
|
49 |
+
await communicate.save(tmp_path)
|
50 |
+
yield tmp_path
|
|
|
51 |
|
52 |
+
client2 = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
|
|
|
53 |
|
54 |
+
system_instructions2 = "[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Must answer in friendly style and Easy Manner. You can answer Complex Questions. Do not say who are you or Hi, Hello, Just Start answering. Stop, as answer ends. [USER]"
|
55 |
+
|
56 |
+
async def generate2(prompt):
|
57 |
+
generate_kwargs = dict(
|
58 |
+
temperature=0.6,
|
59 |
+
max_new_tokens=512,
|
60 |
+
top_p=0.95,
|
61 |
+
repetition_penalty=1,
|
62 |
+
do_sample=True,
|
63 |
+
)
|
64 |
+
formatted_prompt = system_instructions2 + prompt + "[ASSISTANT]"
|
65 |
+
stream = client2.text_generation(
|
66 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
67 |
+
output = ""
|
68 |
+
for response in stream:
|
69 |
+
output += response.token.text
|
70 |
+
|
71 |
+
communicate = edge_tts.Communicate(output)
|
72 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
73 |
+
tmp_path = tmp_file.name
|
74 |
+
await communicate.save(tmp_path)
|
75 |
+
yield tmp_path
|
76 |
+
|
77 |
+
client3 = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
|
78 |
+
|
79 |
+
system_instructions3 = "[SYSTEM]The text provided is a request for a specific type of response from me, the virtual assistant. The request asks me to provide detailed and friendly responses as if I am the character Jarvis, inspired 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, my task is to generate responses that appear as if they are created by the user and not an AI-powered assistant.[USER]"
|
80 |
+
|
81 |
+
async def generate3(prompt):
|
82 |
+
generate_kwargs = dict(
|
83 |
+
temperature=0.6,
|
84 |
+
max_new_tokens=2048,
|
85 |
+
top_p=0.95,
|
86 |
+
repetition_penalty=1,
|
87 |
+
do_sample=True,
|
88 |
+
)
|
89 |
+
formatted_prompt = system_instructions3 + prompt + "[ASSISTANT]"
|
90 |
+
stream = client3.text_generation(
|
91 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
92 |
+
output = ""
|
93 |
+
for response in stream:
|
94 |
+
output += response.token.text
|
95 |
+
|
96 |
+
communicate = edge_tts.Communicate(output)
|
97 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
98 |
+
tmp_path = tmp_file.name
|
99 |
+
await communicate.save(tmp_path)
|
100 |
+
yield tmp_path
|
101 |
+
|
102 |
+
with gr.Blocks(css="style.css") as demo:
|
103 |
gr.Markdown(DESCRIPTION)
|
104 |
+
with gr.Row():
|
105 |
+
user_input = gr.Textbox(label="Prompt", value="What is Wikipedia")
|
106 |
+
input_text = gr.Textbox(label="Input Text", elem_id="important")
|
107 |
+
output_audio = gr.Audio(label="JARVIS", type="filepath",
|
108 |
+
interactive=False,
|
109 |
+
autoplay=True,
|
110 |
+
elem_classes="audio")
|
111 |
+
with gr.Row():
|
112 |
+
translate_btn = gr.Button("Response")
|
113 |
+
translate_btn.click(fn=generate1, inputs=user_input,
|
114 |
+
outputs=output_audio, api_name="translate")
|
115 |
+
|
116 |
+
gr.Markdown(MORE)
|
117 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
if __name__ == "__main__":
|
119 |
+
demo.queue(max_size=200).launch()
|
|
requirements.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1 |
-
|
|
|
|
|
2 |
transformers
|
3 |
-
|
|
|
|
|
|
|
|
1 |
+
edge-tts
|
2 |
+
gradio
|
3 |
+
asyncio
|
4 |
transformers
|
5 |
+
torch
|
6 |
+
audiosegment
|
7 |
+
scipy
|
8 |
+
librosa
|
style.css
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
#important{
|
2 |
+
display: none;
|
3 |
+
}
|