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 | |
from fastapi import Form | |
class Query(BaseModel): | |
text: 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) | |
def generate_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" | |