Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
import os
|
4 |
-
import torch
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
"""
|
13 |
-
raven_pipeline = pipeline(
|
14 |
-
"text-generation",
|
15 |
-
model="Nexusflow/NexusRaven-V2-13B",
|
16 |
-
torch_dtype="auto",
|
17 |
-
device_map="auto",
|
18 |
-
)
|
19 |
|
20 |
-
|
21 |
-
def __init__(self):
|
22 |
-
self.raven_pipeline = raven_pipeline
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
def
|
31 |
-
|
32 |
-
with gr.Blocks() as app:
|
33 |
-
gr.Markdown(title)
|
34 |
-
gr.Markdown(description)
|
35 |
-
with gr.Row():
|
36 |
-
input_text = gr.Textbox(label="Input Text")
|
37 |
-
submit_button = gr.Button("Submit")
|
38 |
-
output_text = gr.Textbox(label="Nexus🐦⬛Raven")
|
39 |
-
submit_button.click(converter.process_text, inputs=input_text, outputs=output_text)
|
40 |
-
return app
|
41 |
|
42 |
-
if __name__ == "__main__":
|
43 |
-
converter = DialogueToSpeechConverter()
|
44 |
-
demo = gr.Interface(
|
45 |
-
fn=converter.process_text,
|
46 |
-
inputs="text",
|
47 |
-
outputs="text",
|
48 |
-
examples=[
|
49 |
-
['''
|
50 |
-
Function:
|
51 |
-
def create_audio_sequence_order(text):
|
52 |
-
"""
|
53 |
-
Analyzes the text and creates an order for each character and narrator segment.
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
str: The path to the generated speech MP3 file.
|
72 |
-
"""
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
use either speech to single voice if there's no dialogue or create_audio_sequence_order if there is dialogue<human_end>
|
77 |
-
''']
|
78 |
-
],
|
79 |
-
title=title,
|
80 |
-
description=description
|
81 |
-
)
|
82 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torch import Tensor
|
5 |
+
from transformers import AutoTokenizer, AutoModel
|
6 |
import gradio as gr
|
|
|
7 |
import os
|
|
|
8 |
|
9 |
+
title = """
|
10 |
+
# 👋🏻Welcome to 🙋🏻♂️Tonic's 🐣e5-mistral🛌🏻Embeddings """
|
11 |
+
description = """
|
12 |
+
You can use this Space to test out the current model [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct). e5mistral has a larger context window, a different prompting/return mechanism and generally better results than other embedding models.
|
13 |
+
You can also use 🐣e5-mistral🛌🏻 by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/e5?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
|
14 |
+
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community on 👻Discord: [![Let's build the future of AI together! 🚀🤖](https://discordapp.com/api/guilds/1109943800132010065/widget.png)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly)
|
15 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:20'
|
|
|
|
|
18 |
|
19 |
+
def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
|
20 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
21 |
+
if left_padding:
|
22 |
+
return last_hidden_states[:, -1]
|
23 |
+
else:
|
24 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
25 |
+
batch_size = last_hidden_states.shape[0]
|
26 |
+
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
27 |
|
28 |
+
def get_detailed_instruct(task_description: str, query: str) -> str:
|
29 |
+
return f'Instruct: {task_description}\nQuery: {query}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
@spaces.GPU
|
33 |
+
def compute_embeddings(*input_texts):
|
34 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
35 |
+
torch.backends.cudnn.allow_tf32 = True
|
36 |
+
torch.backends.cudnn.benchmark = True
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
38 |
+
model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
+
model.to(device)
|
41 |
+
max_length = 4096
|
42 |
+
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
43 |
|
44 |
+
processed_texts = [get_detailed_instruct(task, text) for text in input_texts]
|
45 |
+
batch_dict = tokenizer(processed_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
|
46 |
+
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
47 |
+
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
48 |
+
outputs = model(**batch_dict)
|
49 |
+
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
50 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
51 |
+
embeddings_list = embeddings.detach().cpu().numpy().tolist()
|
52 |
+
return embeddings_list
|
53 |
+
|
54 |
+
def app_interface():
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown(title)
|
57 |
+
gr.Markdown(description)
|
58 |
+
|
59 |
+
input_text_boxes = [gr.Textbox(label=f"Input Text {i+1}") for i in range(4)]
|
60 |
+
|
61 |
+
compute_button = gr.Button("Compute Embeddings")
|
62 |
+
|
63 |
+
output_display = gr.Dataframe(headers=["Embedding Value"], datatype=["number"])
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
for text_box in input_text_boxes:
|
68 |
+
text_box
|
69 |
+
with gr.Column():
|
70 |
+
compute_button
|
71 |
+
output_display
|
72 |
|
73 |
+
compute_button.click(
|
74 |
+
fn=compute_embeddings,
|
75 |
+
inputs=input_text_boxes,
|
76 |
+
outputs=output_display
|
77 |
+
)
|
78 |
|
79 |
+
return demo
|
|
|
|
|
80 |
|
81 |
+
# Run the Gradio app
|
82 |
+
app_interface().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|