Tonic commited on
Commit
8f6035e
1 Parent(s): 7b756ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -69
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
- # Set max_split_size_mb
7
- os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:50'
8
- title = """# 🙋🏻‍♂️Welcome to🌟Tonic's Nexus🐦‍⬛Raven"""
9
- description = """You can build with this endpoint using Nexus Raven. The demo is still a work in progress but we hope to add some endpoints for commonly used functions such as intention mappers and audiobook processing.
10
- You can also use Nexus🐦‍⬛Raven on your laptop & by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic1/NexusRaven2?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>
11
- Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community on 👻Discord: [Discord](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 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)
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
- class DialogueToSpeechConverter:
21
- def __init__(self):
22
- self.raven_pipeline = raven_pipeline
23
 
24
- def process_text(self, input_text: str) -> str:
25
- prompt = f"User Query: {input_text}<human_end>"
26
- result = self.raven_pipeline(prompt, max_new_tokens=2048, return_full_text=False, do_sample=False, temperature=0.001)[0]["generated_text"]
27
- torch.cuda.empty_cache()
28
- return result
 
 
 
29
 
30
- def create_interface():
31
- converter = DialogueToSpeechConverter()
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
- Args:
56
- text (str): The text containing the dialogues and narration.
 
 
 
 
 
 
 
 
 
57
 
58
- Returns:
59
- list: A list of tuples, each containing the character/narrator name and a segment of their dialogue/narration.
60
- """
61
- Function:
62
- def convert_text_to_speech_single_voice(text, voice):
63
- """
64
- Converts a given text to speech using a specified voice. This function is used when there is only one character in the text.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- Args:
67
- text (str): The text to be converted to speech.
68
- voice (str): The voice to be used for the audio generation.
 
 
69
 
70
- Returns:
71
- str: The path to the generated speech MP3 file.
72
- """
73
 
74
- User Query: Currently, one way that the wealthy distinguish themselves from others is through the collection of rare objects. In a Celebration Society, to own an “original” of something will remain significant. However, barring a desire to prevent others from enjoying the experience, it will become possible to have perfect replicas of all manner of objects including paintings and sculptures.
75
- There will still be pride of ownership in the original. Others will be able to fully enjoy the “same” piece as well.
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()