Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Create inference client for your model (no need for token if model is public)
|
| 5 |
client = InferenceClient("dlucidone/kumaoni-mbart-lora")
|
| 6 |
|
| 7 |
-
def translate(text):
|
| 8 |
if not text.strip():
|
| 9 |
return "Please enter text."
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
return f"Error: {e}"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
fn=translate,
|
| 20 |
-
inputs=gr.Textbox(label="text", placeholder="Enter text to translate"),
|
| 21 |
-
outputs=gr.Textbox(label="output"),
|
| 22 |
-
title="Kumaoni Translator API"
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from gradio.routes import mount_gradio_app
|
| 5 |
|
|
|
|
| 6 |
client = InferenceClient("dlucidone/kumaoni-mbart-lora")
|
| 7 |
|
| 8 |
+
def translate(text: str):
|
| 9 |
if not text.strip():
|
| 10 |
return "Please enter text."
|
| 11 |
try:
|
| 12 |
+
response = client.post(json={"inputs": text})
|
| 13 |
+
if isinstance(response, list) and len(response) and "generated_text" in response[0]:
|
| 14 |
+
return response[0]["generated_text"]
|
| 15 |
+
return str(response)
|
| 16 |
except Exception as e:
|
| 17 |
return f"Error: {e}"
|
| 18 |
|
| 19 |
+
# Create a FastAPI app
|
| 20 |
+
api = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Add /predict endpoint
|
| 23 |
+
@api.post("/predict")
|
| 24 |
+
async def predict(request: Request):
|
| 25 |
+
body = await request.json()
|
| 26 |
+
text = body.get("text", "")
|
| 27 |
+
return {"translation": translate(text)}
|
| 28 |
+
|
| 29 |
+
# Mount Gradio interface for human testing
|
| 30 |
+
gr_app = gr.Interface(fn=translate, inputs="text", outputs="text", title="Kumaoni Translator")
|
| 31 |
+
app = mount_gradio_app(api, gr_app, path="/")
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
import uvicorn
|
| 35 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|