Spaces:
Sleeping
Sleeping
File size: 3,841 Bytes
eca8f34 2eabb0b eca8f34 2eabb0b eca8f34 2eabb0b eca8f34 2eabb0b eca8f34 2eabb0b eca8f34 2eabb0b eca8f34 2eabb0b |
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 |
# import firebase_admin
# from firebase_admin import credentials
# from firebase_admin import firestore
import io
from fastapi import FastAPI, File, UploadFile
from werkzeug.utils import secure_filename
# import speech_recognition as sr
import subprocess
import os
import requests
import random
import pandas as pd
from pydub import AudioSegment
from datetime import datetime
from datetime import date
import numpy as np
# from sklearn.ensemble import RandomForestRegressor
import shutil
import json
# from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
from pydantic import BaseModel
from typing import Annotated
# from transformers import BertTokenizerFast, EncoderDecoderModel
import torch
import re
# from transformers import AutoTokenizer, T5ForConditionalGeneration
from fastapi import Form
# from transformers import AutoModelForSequenceClassification
# from transformers import TFAutoModelForSequenceClassification
# from transformers import AutoTokenizer, AutoConfig
import numpy as np
import threading
import random
import string
import time
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline
device = "cpu"
tokenizer = AutoTokenizer.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")
model = AutoModelForSeq2SeqLM.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base").to(device)
def paraphrase(
question,
num_beams=5,
num_beam_groups=5,
num_return_sequences=1,
repetition_penalty=10.0,
diversity_penalty=3.0,
no_repeat_ngram_size=2,
temperature=0.7,
max_length=10000
):
input_ids = tokenizer(
f'paraphrase: {question}',
return_tensors="pt", padding="longest",
max_length=max_length,
truncation=True,
).input_ids
outputs = model.generate(
input_ids, temperature=temperature, repetition_penalty=repetition_penalty,
num_return_sequences=num_return_sequences, no_repeat_ngram_size=no_repeat_ngram_size,
num_beams=num_beams, num_beam_groups=num_beam_groups,
max_length=max_length, diversity_penalty=diversity_penalty
)
res = tokenizer.batch_decode(outputs, skip_special_tokens=True)
return res
class Query(BaseModel):
text: str
class Query2(BaseModel):
text: str
host:str
from fastapi import FastAPI, Request, Depends, UploadFile, File
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
# now = datetime.now()
# UPLOAD_FOLDER = '/files'
# ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png',
# 'jpg', 'jpeg', 'gif', 'ogg', 'mp3', 'wav'}
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")
@app.post("/")
async def get_answer(q: Query ):
text = q.text
x= paraphrase(text)
return x[0]
@app.post("/large")
async def get_answer2(q: Query2 ):
text = q.text
host= q.host
N = 20
res = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=N))
res= res+ str(time.time())
id= res
t = threading.Thread(target=do_ML, args=(id,text,host))
t.start()
return JSONResponse({"id":id})
def do_ML(id:str,text:str,host:str):
try:
x= paraphrase(text)
result=x[0]
data={"id":id,"result":result}
x=requests.post(host,data= data)
print(x.text)
except:
print("Error occured id="+id)
|