Spaces:
Runtime error
Runtime error
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 threading | |
import random | |
import string | |
import time | |
from fastapi import Form | |
class Query(BaseModel): | |
text: str | |
class Query2(BaseModel): | |
text: str | |
host:str | |
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
# tokenizer = BertTokenizerFast.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization') | |
# model = EncoderDecoderModel.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization').to(device) | |
summarizer = pipeline( | |
"summarization", | |
"pszemraj/long-t5-tglobal-base-16384-book-summary", | |
device=0 if torch.cuda.is_available() else -1, | |
) | |
def generate_summary(text): | |
result = summarizer(text) | |
return result[0]["summary_text"] | |
# cut off at BERT max length 512 | |
# inputs = tokenizer([text], padding="max_length", truncation=True, max_length=512, return_tensors="pt") | |
# input_ids = inputs.input_ids.to(device) | |
# attention_mask = inputs.attention_mask.to(device) | |
# output = model.generate(input_ids, attention_mask=attention_mask) | |
# return tokenizer.decode(output[0], skip_special_tokens=True) | |
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') | |
async def startup_event(): | |
print("on startup") | |
async def get_answer(q: Query ): | |
long_text = q.text | |
r= generate_summary(long_text) | |
return r | |
return "hello" | |
async def get_answer(q: Query2 ): | |
N = 20 | |
res = ''.join(random.choices(string.ascii_uppercase + | |
string.digits, k=N)) | |
res= res+ str(time.time()) | |
id= res | |
text = q.text | |
host= q.host | |
t = threading.Thread(target=do_ML, args=(id,text,host)) | |
t.start() | |
return JSONResponse({"id":id}) | |
return "hello" | |
import requests | |
def do_ML(id:str,long_text:str,host:str): | |
try: | |
r= generate_summary(long_text) | |
data={"id":id,"result":r} | |
x=requests.post(host,data= data) | |
print(x.text) | |
except: | |
print("Error occured id= "+id) | |