Spaces:
Sleeping
Sleeping
import sys | |
from pathlib import Path | |
import os | |
sys.path.append(str(Path(__file__).resolve().parent.parent)) | |
#print(sys.path) | |
from typing import Any | |
from fastapi import FastAPI, Request, APIRouter, Form | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.templating import Jinja2Templates | |
from fastapi.middleware.cors import CORSMiddleware | |
from app.config import settings | |
from app import __version__ | |
from app.pyfiles import subject_gen | |
from app.pyfiles import qa_gen | |
import numpy as np | |
import pandas as pd | |
# from transformers import GPT2Tokenizer ,GPT2Model | |
current_path = os.path.dirname(os.path.abspath(__file__)) | |
# model = GPT2Model.from_pretrained("gpt2") | |
# tokenizer = GPT2Tokenizer.from_pretrained("gpt2") | |
# checkpoint1 = os.path.join(current_path, "pyfiles") | |
# checkpoint = os.path.join(checkpoint1, "gpt_tokenizer") | |
# model.save_pretrained(checkpoint) | |
# tokenizer.save_pretrained(checkpoint) | |
app = FastAPI( | |
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json" | |
) | |
# To access Templates directory | |
templates = Jinja2Templates(directory="app/templates") | |
email_inp = None | |
#################################### Home Page endpoints ################################################# | |
async def root(request: Request): | |
return templates.TemplateResponse("index.html", {'request': request,}) | |
async def email_input_root(request: Request): | |
return templates.TemplateResponse("email_input.html", {'request': request,}) | |
async def create_upload_files(request: Request , paragraphInput: str = Form(...)): | |
print("hi im in subject gen", paragraphInput ) | |
result = subject_gen.subject_gen_func(paragraphInput) | |
print(result) | |
if(len(result.split("<subject>"))>1): | |
subject = result.split("<subject>")[1].replace("<eos>","") | |
else : | |
subject = "Please Try Again" | |
return templates.TemplateResponse("subject_generation.html", {"request": request, | |
"result":subject, | |
"email":paragraphInput,}) | |
################################################################################################################### | |
async def question_input_root(request: Request): | |
return templates.TemplateResponse("question_input.html", {'request': request,}) | |
async def question_awnser(request: Request , questionInput: str = Form(...)): | |
# print("hi im in subject gen", paragraphInput ) | |
result = qa_gen.question_awnser(questionInput) | |
# print(result) | |
answer = result | |
# if(len(result)>1): | |
# awnser = result | |
# else : | |
# awnser = "Please Try Again" | |
# return templates.TemplateResponse("awnser_generation.html", {"request": request, | |
# "result":awnser, | |
# "question":questionInput,}) | |
# Set all CORS enabled origins | |
if settings.BACKEND_CORS_ORIGINS: | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Start app | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8001) |