File size: 4,406 Bytes
9dd099e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8b733f
9dd099e
d8b733f
 
 
9dd099e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a3eb0b
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
import os
import shutil
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import requests
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores.faiss import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
import google.generativeai as genai
from dotenv import load_dotenv

app = FastAPI()

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load environment variables
load_dotenv()

# Configure Google API
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

class QuestionInput(BaseModel):
    question: str

class UploadInput(BaseModel):
    url: str = Form(None)

def scrape_data(url):

    return "scraped data"

def split_text_into_chunks(text):
    splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    text_chunks = splitter.split_text(text)
    return text_chunks

def create_vector_store(chunks):
    embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
    vector_store = FAISS.from_texts(chunks, embedding=embeddings)
    vector_store.save_local("faiss_index")

def setup_conversation_chain(template):
    model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
    prompt = PromptTemplate(template=template, input_variables=["context", "question"])
    chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
    return chain

@app.post("/upload")
async def upload_files(url: str = Form(None)):
    try:
        # print(url)
        # all_text = ""
        
        # # Process URL
        # if url:
        #     # check if url is valid (request doesnt give error)
        #     if # doesnt give error
        #         all_text = scrape_data(url)
        #     else:
        #         raise HTTPException(status_code=400, detail="Invalid URL")

        # if not all_text:
        #     raise HTTPException(status_code=400, detail="No content to process")

        # chunks = split_text_into_chunks(all_text)
        # create_vector_store(chunks)

        return {"message": "Content uploaded and processed successfully"}
    except HTTPException as http_exc:
        print(f"HTTP Exception: {http_exc.detail}")
        raise http_exc
    except Exception as e:
        print(f"Unhandled Exception: {e}")
        raise HTTPException(status_code=500, detail=str(e))
    

@app.post("/ask")
async def ask_question(question_input: QuestionInput):
    try:
        embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
        indexed_data = FAISS.load_local("reviews_index", embeddings, allow_dangerous_deserialization=True)
        docs = indexed_data.similarity_search(question_input.question)
        
        prompt_template = """
        Your alias is AI Rate Professor. Your task is to provide a thorough response based on the given context, ensuring all relevant details are included. 
        If the requested information isn't available, simply state, "answer not available in context," then answer based on your understanding, connecting with the context. 
        Don't provide incorrect information.\n\n
        Context: \n {context}?\n
        Question: \n {question}\n
        Answer:
        """
        
        chain = setup_conversation_chain(prompt_template)
        response = chain({"input_documents": docs, "question": question_input.question}, return_only_outputs=True)
        
        print(response["output_text"])
        return {"answer": response["output_text"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# prompt_template = """
#         Your alias is AI Rate Professor. Your task is to provide a thorough response based on the given context, ensuring all relevant details are included. 
#         If the requested information isn't available, simply state, "answer not available in context," then answer based on your understanding, connecting with the context. 
#         Don't provide incorrect information.\n\n
#         Context: \n {context}?\n
#         Question: \n {question}\n
#         Answer:
#         """