File size: 4,797 Bytes
651529c
 
 
 
 
 
 
 
 
 
 
 
2f4ebfa
651529c
 
 
 
 
 
 
 
83bdac1
a7d2964
 
 
83bdac1
651529c
2f4ebfa
 
 
 
 
 
651529c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f4ebfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f7a87
 
a7d2964
 
e0f7a87
 
 
33b17d4
e0f7a87
 
 
0a47dab
e0f7a87
 
 
 
 
 
 
 
 
 
 
 
c3adc49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651529c
 
 
 
a4804b8
ec5749b
 
 
 
2d41de3
ec5749b
 
 
 
 
 
 
 
2f4ebfa
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import io
from fastapi import FastAPI, File, UploadFile
import subprocess
import os
import requests
import random
from datetime import datetime
from datetime import date
import json
from pydantic import BaseModel
from typing import Annotated
import random
from fastapi import FastAPI, Response
import string
import time
from huggingface_hub import InferenceClient

from fastapi import Form

class Query(BaseModel):
    text: str
    
    
class Query2(BaseModel):
    text: str
  
   
class QueryM(BaseModel):
    text: str
    tokens:int
    temp:float
    topp:float
    topk:float



from fastapi import FastAPI, Request, Depends, UploadFile, File
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse


app = FastAPI()

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


# cred = credentials.Certificate('key.json')
# app1 = firebase_admin.initialize_app(cred)
# db = firestore.client()
# data_frame = pd.read_csv('data.csv')



@app.on_event("startup")
async def startup_event():
    print("on startup")
    # requests.get("https://audiospace-1-u9912847.deta.app/sendcode")

audio_space="https://audiospace-1-u9912847.deta.app/uphoto"

import threading
from huggingface_hub.inference_api import InferenceApi
client = InferenceClient()


@app.post("/image")
async def get_answer(q: Query ):
    text = q.text
    try:
        global client
        imagei = client.text_to_image(text)
        byte_array = io.BytesIO()
        imagei.save(byte_array, format='JPEG')
        response = Response(content=byte_array.getvalue(), media_type="image/png")
        return response
    
    except:
        return JSONResponse({"status":False})
    
    
@app.post("/mistral")
async def get_answer(q: QueryM ):
    text = q.text
    try:
        client = InferenceClient()
        generate_kwargs = dict(
        max_new_tokens= int(q.tokens),
        do_sample=True,
        top_p= q.topp,
        top_k=int(q.topk),
        temperature=q.temp,
        )
        inputs= text
        response = client.post(json={"inputs": inputs, "parameters": generate_kwargs}, model="mistralai/Mistral-7B-Instruct-v0.1")
        json_string = response.decode('utf-8')
        list_of_dicts = json.loads(json_string)
        result_dict = list_of_dicts[0]
        x=(result_dict['generated_text'])
        x=x.replace(inputs,'')
        return JSONResponse({"result":x,"status":True})    
    except Exception as e:
        print(e)
        return JSONResponse({"status":False})
    
    


@app.post("/zephyr")
async def get_answer(q: QueryM ):
    text = q.text
    try:
        client = InferenceClient()
        generate_kwargs = dict(
        max_new_tokens= int(q.tokens),
        repetition_penalty=1.0,
        top_p= q.topp,
        top_k=int(q.topk),
        temperature=q.temp,
        stop= ["</s>", "<|>"]
        )
        inputs= text
        response = client.post(json={"inputs": inputs, "parameters": generate_kwargs}, model="mistralai/Mistral-7B-Instruct-v0.1")
        json_string = response.decode('utf-8')
        list_of_dicts = json.loads(json_string)
        result_dict = list_of_dicts[0]
        x=(result_dict['generated_text'])
        x=x.replace(inputs,'')
        return JSONResponse({"result":x,"status":True})    
    except Exception as e:
        print(e)
        return JSONResponse({"status":False})


@app.post("/openchat35")
async def get_answer(q: QueryM ):
    text = q.text
    try:
        client = InferenceClient()
        generate_kwargs = dict(
        max_new_tokens= int(q.tokens),
        repetition_penalty=1.0,
        top_p= q.topp,
        top_k=int(q.topk),
        temperature=q.temp,
        stop= ["<|end_of_turn|>", "<??|>"]
        )
        inputs= text
        response = client.post(json={"inputs": inputs, "parameters": generate_kwargs},model="openchat/openchat_3.5")
        json_string = response.decode('utf-8')
        list_of_dicts = json.loads(json_string)
        result_dict = list_of_dicts[0]
        x=(result_dict['generated_text'])
        x=x.replace(inputs,'')
        return JSONResponse({"result":x,"status":True})    
    except Exception as e:
        print(e)
        return JSONResponse({"status":False})




@app.post("/image_new")
async def get_answer(q: Query ):
    text = q.text
    try:
        global client
        imagei = client.text_to_image(text, model ='openskyml/dalle-3-xl')
        byte_array = io.BytesIO()
        imagei.save(byte_array, format='JPEG')
        response = Response(content=byte_array.getvalue(), media_type="image/png")
        return response
    
    except:
        return JSONResponse({"status":False})