Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
title = """# 👋🏻Welcome To 🌟Tonic's🌐Aya-101"""
|
7 |
+
description = """ try this space to build downstream applications with [CohereForAI/aya-101](https://huggingface.co/CohereForAI/aya-101) use via API ;-) """
|
8 |
+
|
9 |
+
checkpoint = "CohereForAI/aya-101"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
11 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
|
12 |
+
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
model.to(device)
|
15 |
+
|
16 |
+
if device == "cuda":
|
17 |
+
model = model.half()
|
18 |
+
|
19 |
+
@spaces.GPU
|
20 |
+
def translate(text):
|
21 |
+
"""
|
22 |
+
Translates the input text to English using the Aya model.
|
23 |
+
Assumes the model can automatically detect the input language.
|
24 |
+
"""
|
25 |
+
inputs = tokenizer.encode(text, return_tensors="pt").to(device)
|
26 |
+
|
27 |
+
outputs = model.generate(inputs, max_new_tokens=128)
|
28 |
+
|
29 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
+
return translation
|
31 |
+
|
32 |
+
def main():
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown(title)
|
35 |
+
gr.Markdown(description)
|
36 |
+
with gr.Row():
|
37 |
+
input_text = gr.Textbox(label="Input Text")
|
38 |
+
output_text = gr.Textbox(label="🌐Aya", interactive=False)
|
39 |
+
input_text.change(fn=translate, inputs=input_text, outputs=output_text)
|
40 |
+
|
41 |
+
demo.launch()
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|