File size: 1,310 Bytes
581f159
 
 
 
 
 
 
 
 
 
 
58190b4
581f159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58190b4
 
 
 
 
581f159
 
58190b4
 
 
 
 
 
 
0be9b92
58190b4
a0226d6
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import sys
import time
import signal
import io

from fastapi import FastAPI, Request, status, Form, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, StreamingResponse
import fn
import gradio as gr
from app import demo

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

gr.mount_gradio_app(app, demo, path="/gradio")

@app.post("/set_config")
async def api_set_config(args: dict):
    content = fn.set_config_args(args)
    return {'content': content}

@app.post("/infer")
async def api_infer(args: dict):
    args['fastapi'] = True
    if 'stream' in args and args['stream']:
        return StreamingResponse(
            fn.chat(args['input'], [], args['instruct'], args),
            media_type="text/event-stream",
        )
    else:
        content = fn.infer(args['input'], [], args['instruct'], args)
        return {'content': content}

@app.post("/numel")
async def api_numel(args: dict):
    content = fn.numel(args['input'], [], args['instruct'], args)
    return {'numel': content}