add api
Browse files- api.py +22 -0
- app.py +15 -13
- requirements.txt +1 -1
api.py
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
import argparse
|
| 3 |
+
import random
|
| 4 |
+
from fastapi import FastAPI, Form
|
| 5 |
+
from fastapi.responses import Response
|
| 6 |
+
|
| 7 |
+
parser = argparse.ArgumentParser(description="Chat API")
|
| 8 |
+
parser.add_argument("--port", type=int, default=10024, help="Port")
|
| 9 |
+
args = parser.parse_args()
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.post("/chat")
|
| 15 |
+
async def chat_endpoint(text: str = Form(...)):
|
| 16 |
+
print("Input text:", text)
|
| 17 |
+
response = random.choice(["hello?", "hello world!"])
|
| 18 |
+
return Response(content=response)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
if __name__ == '__main__':
|
| 22 |
+
uvicorn.run(app, host="0.0.0.0", port=args.port)
|
app.py
CHANGED
|
@@ -1,16 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def get_responce(msg):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return result["translation"][0]
|
| 12 |
# pass
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with gr.Blocks() as demo:
|
| 15 |
chatbot = gr.Chatbot()
|
| 16 |
msg = gr.Textbox()
|
|
@@ -26,10 +35,3 @@ with gr.Blocks() as demo:
|
|
| 26 |
|
| 27 |
if __name__ == "__main__":
|
| 28 |
demo.launch()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
url = os.environ.get("URL", None)
|
| 7 |
+
# url = "http://localhost:10024/chat"
|
| 8 |
+
# from apidemo.TranslateDemo import createRequest
|
| 9 |
+
|
| 10 |
|
| 11 |
def get_responce(msg):
|
| 12 |
+
# result_str = createRequest(msg)
|
| 13 |
+
# result = json.loads(result_str)
|
| 14 |
+
# return result["translation"][0]
|
|
|
|
| 15 |
# pass
|
| 16 |
|
| 17 |
+
data = {"text": msg}
|
| 18 |
+
response = requests.post(url, data=data)
|
| 19 |
+
print(response.content.decode('utf-8'))
|
| 20 |
+
return response.content.decode('utf-8')
|
| 21 |
+
|
| 22 |
+
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
chatbot = gr.Chatbot()
|
| 25 |
msg = gr.Textbox()
|
|
|
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
websocket
|
|
|
|
| 1 |
+
websocket
|