Create api.py
Browse files
api.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import T5ForConditionalGeneration,T5Tokenizer
|
4 |
+
import soundfile as sf
|
5 |
+
import librosa
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
import requests
|
9 |
+
import json
|
10 |
+
|
11 |
+
API_TOKEN = 'your-api-token' # 你在Hugging Face网站上的API令牌
|
12 |
+
API_URL = 'https://api-inference.huggingface.co/models/gpt2' # 根据你的模型选择合适的URL
|
13 |
+
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
16 |
+
}
|
17 |
+
|
18 |
+
def generate(inp):
|
19 |
+
data = json.dumps({"inputs": inp})
|
20 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
21 |
+
return json.loads(response.content.decode("utf-8"))[0]['generated_text']
|
22 |
+
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=generate,
|
25 |
+
inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
|
26 |
+
outputs="text",
|
27 |
+
title="Text-to-Speech",
|
28 |
+
description="Convert your text into speech!",
|
29 |
+
)
|
30 |
+
iface.launch()
|