Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
import onnxruntime as ort
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
# Initialize FastAPI and Gradio
|
| 9 |
-
app = FastAPI()
|
| 10 |
|
| 11 |
# Load ONNX model and tokenizer
|
| 12 |
MODEL_FILE = "./model.onnx"
|
| 13 |
session = ort.InferenceSession(MODEL_FILE)
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
input_text: str
|
| 19 |
-
|
| 20 |
-
# FastAPI endpoint for model prediction
|
| 21 |
-
@app.post("/predict")
|
| 22 |
-
async def predict(translation_input: TranslationInput):
|
| 23 |
-
"""
|
| 24 |
-
Endpoint for inference.
|
| 25 |
-
:param translation_input: Text input in English.
|
| 26 |
-
:return: Translated text in French.
|
| 27 |
-
"""
|
| 28 |
# Tokenize input text
|
| 29 |
tokenized_input = tokenizer(
|
| 30 |
-
|
| 31 |
return_tensors="np",
|
| 32 |
padding=True
|
| 33 |
)
|
|
@@ -41,20 +25,12 @@ async def predict(translation_input: TranslationInput):
|
|
| 41 |
|
| 42 |
# Decode output and return translated text
|
| 43 |
translated_text = tokenizer.decode(outputs[0][0], skip_special_tokens=True)
|
| 44 |
-
return
|
| 45 |
-
|
| 46 |
-
# Gradio Interface
|
| 47 |
-
def gradio_predict(input_text):
|
| 48 |
-
response = predict(TranslationInput(input_text=input_text))
|
| 49 |
-
return response["translated_text"]
|
| 50 |
|
| 51 |
# Gradio interface for the web app
|
| 52 |
-
|
| 53 |
fn=gradio_predict,
|
| 54 |
inputs="text",
|
| 55 |
outputs="text",
|
| 56 |
live=True
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# Launch Gradio app
|
| 60 |
-
gradio_interface.launch(inline=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import onnxruntime as ort
|
| 3 |
from transformers import AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load ONNX model and tokenizer
|
| 6 |
MODEL_FILE = "./model.onnx"
|
| 7 |
session = ort.InferenceSession(MODEL_FILE)
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
|
| 9 |
|
| 10 |
+
# Gradio prediction function
|
| 11 |
+
def gradio_predict(input_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Tokenize input text
|
| 13 |
tokenized_input = tokenizer(
|
| 14 |
+
input_text,
|
| 15 |
return_tensors="np",
|
| 16 |
padding=True
|
| 17 |
)
|
|
|
|
| 25 |
|
| 26 |
# Decode output and return translated text
|
| 27 |
translated_text = tokenizer.decode(outputs[0][0], skip_special_tokens=True)
|
| 28 |
+
return translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Gradio interface for the web app
|
| 31 |
+
gr.Interface(
|
| 32 |
fn=gradio_predict,
|
| 33 |
inputs="text",
|
| 34 |
outputs="text",
|
| 35 |
live=True
|
| 36 |
+
).launch(share=True)
|
|
|
|
|
|
|
|
|