Spaces:
Build error
Build error
Upload 3 files
Browse filesFiles changed (3)
- main.pyAdded+118
- reviews.jsonAdded+689
- util.pyAdded+51
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import 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:
# """123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689{
"professors": [
{
"professor_id": "A1B2C3",
"name": "Dr. A. Kumar",
"course": "Data Structures",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Kumar explains data structures with great clarity and provides excellent examples."
},
{
"rating": 3.7,
"review_text": "The course content was good, but the pace was a bit fast for beginners."
},
{
"rating": 4.8,
"review_text": "One of the best professors I've had. Very knowledgeable and approachable."
}
]
},
{
"professor_id": "D4E5F6",
"name": "Dr. S. Reddy",
"course": "Algorithms",
"reviews": [
{
"rating": 4.2,
"review_text": "Dr. Reddy's lectures are well-organized and engaging. The assignments are challenging but fair."
},
{
"rating": 3.9,
"review_text": "The concepts were well taught, but the grading seemed a bit harsh."
},
{
"rating": 4.7,
"review_text": "Fantastic teacher with a deep understanding of algorithms. Highly recommended."
}
]
},
{
"professor_id": "G7H8I9",
"name": "Dr. P. Sharma",
"course": "Operating Systems",
"reviews": [
{
"rating": 4.0,
"review_text": "Good course, though the material was sometimes a bit dry."
},
{
"rating": 4.3,
"review_text": "Dr. Sharma is very knowledgeable and provides clear explanations."
},
{
"rating": 4.1,
"review_text": "The course was informative, but more interactive sessions would be beneficial."
}
]
},
{
"professor_id": "J1K2L3",
"name": "Dr. L. Patel",
"course": "Database Systems",
"reviews": [
{
"rating": 4.5,
"review_text": "Excellent course with practical insights into database management."
},
{
"rating": 4.0,
"review_text": "Dr. Patel has a solid understanding of database systems but could improve on feedback timeliness."
},
{
"rating": 4.6,
"review_text": "Very engaging and knowledgeable. The practical sessions were very useful."
}
]
},
{
"professor_id": "M4N5O6",
"name": "Dr. V. Singh",
"course": "Computer Networks",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Singh provides a comprehensive overview of networking concepts and practical applications."
},
{
"rating": 3.8,
"review_text": "The course is good, but sometimes the theoretical parts are too lengthy."
},
{
"rating": 4.7,
"review_text": "Highly knowledgeable with great real-world examples and case studies."
}
]
},
{
"professor_id": "P7Q8R9",
"name": "Dr. A. Mehta",
"course": "Software Engineering",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Mehta covers software engineering principles effectively, with a focus on real-life applications."
},
{
"rating": 4.0,
"review_text": "Good insights into software project management, though sometimes the lectures are a bit dry."
},
{
"rating": 4.5,
"review_text": "Engaging lectures and useful project work that helps understand the concepts better."
}
]
},
{
"professor_id": "S1T2U3",
"name": "Dr. R. Joshi",
"course": "Artificial Intelligence",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Joshi is an expert in AI and makes complex topics easier to understand with practical examples."
},
{
"rating": 4.1,
"review_text": "The course is quite advanced, but Dr. Joshi’s explanations help in grasping difficult concepts."
},
{
"rating": 4.7,
"review_text": "Excellent course content with a lot of interactive sessions and real-world AI applications."
}
]
},
{
"professor_id": "V4W5X6",
"name": "Dr. N. Kapoor",
"course": "Machine Learning",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Kapoor provides a solid foundation in machine learning with well-structured coursework."
},
{
"rating": 4.2,
"review_text": "Good course but sometimes the pace of covering topics is too quick."
},
{
"rating": 4.8,
"review_text": "Highly engaging and informative, with a lot of practical exercises and real-world datasets."
}
]
},
{
"professor_id": "Y7Z8A9",
"name": "Dr. G. Rao",
"course": "Web Development",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Rao’s course is well-organized with practical projects that enhance learning."
},
{
"rating": 4.0,
"review_text": "The course covers the essentials, but more advanced topics could be included."
},
{
"rating": 4.6,
"review_text": "Excellent for beginners and intermediate learners, with a lot of hands-on exercises."
}
]
},
{
"professor_id": "B1C2D3",
"name": "Dr. H. Sinha",
"course": "Human-Computer Interaction",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Sinha’s course is insightful with a focus on user experience and interaction design."
},
{
"rating": 4.1,
"review_text": "Interesting content but sometimes the theoretical aspects can be quite dense."
},
{
"rating": 4.5,
"review_text": "Great course with a lot of practical examples and user-centered design principles."
}
]
},
{
"professor_id": "E4F5G6",
"name": "Dr. J. Verma",
"course": "Cybersecurity",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Verma is highly knowledgeable and provides deep insights into cybersecurity threats and solutions."
},
{
"rating": 4.2,
"review_text": "The content is relevant, though some practical exercises could be more hands-on."
},
{
"rating": 4.8,
"review_text": "Excellent understanding of cybersecurity issues with practical case studies."
}
]
},
{
"professor_id": "H7I8J9",
"name": "Dr. M. Bhat",
"course": "Embedded Systems",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Bhat's course is very informative with detailed explanations of embedded systems."
},
{
"rating": 4.0,
"review_text": "The course is good, but more real-world examples would make it even better."
},
{
"rating": 4.5,
"review_text": "Engaging lectures with a lot of practical applications and projects."
}
]
},
{
"professor_id": "K1L2M3",
"name": "Dr. A. Gupta",
"course": "Cloud Computing",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Gupta offers a thorough understanding of cloud computing with practical cloud services."
},
{
"rating": 4.2,
"review_text": "The course is well-structured but could benefit from more interactive sessions."
},
{
"rating": 4.7,
"review_text": "Fantastic insights into cloud technologies and hands-on experience with cloud platforms."
}
]
},
{
"professor_id": "N4O5P6",
"name": "Dr. R. Jain",
"course": "Computer Graphics",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Jain’s course provides a strong foundation in computer graphics with great practical exercises."
},
{
"rating": 4.1,
"review_text": "Interesting content, though the course could be more engaging with interactive elements."
},
{
"rating": 4.6,
"review_text": "Highly recommended for anyone interested in graphics programming. The practical work was excellent."
}
]
},
{
"professor_id": "Q7R8S9",
"name": "Dr. P. Singh",
"course": "Computer Vision",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Singh is an expert in computer vision, and the course material is very relevant and up-to-date."
},
{
"rating": 4.3,
"review_text": "Great course, though it could use more hands-on projects to complement the theory."
},
{
"rating": 4.7,
"review_text": "Excellent course with a lot of practical applications and real-world examples."
}
]
},
{
"professor_id": "R1S2T3",
"name": "Dr. T. Kumar",
"course": "Distributed Systems",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Kumar provides a thorough understanding of distributed systems with practical insights."
},
{
"rating": 4.0,
"review_text": "The course is good but could benefit from more interactive discussions and exercises."
},
{
"rating": 4.5,
"review_text": "Highly knowledgeable with a lot of practical examples and case studies."
}
]
},
{
"professor_id": "U4V5W6",
"name": "Dr. V. Iyer",
"course": "Compiler Design",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Iyer's course is detailed and comprehensive, covering all aspects of compiler design."
},
{
"rating": 4.1,
"review_text": "Theoretical content is well-taught but practical exercises could be improved."
},
{
"rating": 4.6,
"review_text": "Excellent teaching with a strong focus on practical compiler construction techniques."
}
]
},
{
"professor_id": "X7Y8Z9",
"name": "Dr. W. Rao",
"course": "Natural Language Processing",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Rao's expertise in NLP is evident, and the course material is very relevant."
},
{
"rating": 4.2,
"review_text": "Great course with a lot of practical exercises, though the pace is a bit fast."
},
{
"rating": 4.7,
"review_text": "Highly engaging with a lot of real-world NLP applications and examples."
}
]
},
{
"professor_id": "Y1Z2A3",
"name": "Dr. Z. Patel",
"course": "Quantum Computing",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Patel provides an excellent overview of quantum computing with practical insights."
},
{
"rating": 4.1,
"review_text": "The course is very advanced but Dr. Patel’s explanations make it more accessible."
},
{
"rating": 4.8,
"review_text": "Highly recommended for those interested in quantum computing. Great balance of theory and practice."
}
]
},
{
"professor_id": "Z4A5B6",
"name": "Dr. C. Singh",
"course": "Information Retrieval",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Singh’s course on information retrieval is well-structured and informative."
},
{
"rating": 4.0,
"review_text": "Good course content but could use more real-world examples and practical sessions."
},
{
"rating": 4.6,
"review_text": "Excellent teaching with a lot of practical exercises that enhance understanding."
}
]
},
{
"professor_id": "A7B8C9",
"name": "Dr. D. Sharma",
"course": "Digital Logic Design",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Sharma provides a solid foundation in digital logic design with clear explanations."
},
{
"rating": 4.0,
"review_text": "Theoretical parts are well-covered but practical aspects could be more interactive."
},
{
"rating": 4.5,
"review_text": "Very knowledgeable and the practical sessions were very helpful."
}
]
},
{
"professor_id": "B1C2D3",
"name": "Dr. E. Patel",
"course": "Mobile Computing",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Patel’s course is highly relevant with a focus on current mobile computing technologies."
},
{
"rating": 4.2,
"review_text": "Good content but the pace of the course could be adjusted for better understanding."
},
{
"rating": 4.7,
"review_text": "Excellent course with practical insights into mobile application development."
}
]
},
{
"professor_id": "C4D5E6",
"name": "Dr. F. Kumar",
"course": "Network Security",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Kumar’s course is very informative with a lot of practical network security scenarios."
},
{
"rating": 4.1,
"review_text": "Content is relevant, but more case studies would enhance the learning experience."
},
{
"rating": 4.7,
"review_text": "Highly detailed and practical course on network security with a lot of hands-on labs."
}
]
},
{
"professor_id": "D7E8F9",
"name": "Dr. G. Rao",
"course": "Systems Programming",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Rao’s course on systems programming is well-structured and comprehensive."
},
{
"rating": 4.0,
"review_text": "Good theoretical coverage but practical examples could be more in-depth."
},
{
"rating": 4.6,
"review_text": "Excellent course with practical exercises and real-world programming problems."
}
]
},
{
"professor_id": "E1F2G3",
"name": "Dr. H. Gupta",
"course": "Computer Architecture",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Gupta provides a thorough understanding of computer architecture with clear explanations."
},
{
"rating": 4.2,
"review_text": "Good course but some of the concepts are a bit complex for beginners."
},
{
"rating": 4.6,
"review_text": "Highly engaging with practical insights into computer hardware and architecture."
}
]
},
{
"professor_id": "F1G2H3",
"name": "Dr. M. Verma",
"course": "Parallel Computing",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Verma’s course on parallel computing is insightful with practical applications."
},
{
"rating": 4.0,
"review_text": "Good coverage of parallel computing concepts but could use more hands-on labs."
},
{
"rating": 4.5,
"review_text": "Excellent explanations and practical exercises. Highly recommended for advanced learners."
}
]
},
{
"professor_id": "G2H3I4",
"name": "Dr. L. Shah",
"course": "Big Data",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Shah provides a comprehensive overview of big data technologies and tools."
},
{
"rating": 4.1,
"review_text": "Good course, but the pace could be slower for better understanding."
},
{
"rating": 4.6,
"review_text": "Highly engaging with real-world examples and practical applications in big data."
}
]
},
{
"professor_id": "H3I4J5",
"name": "Dr. P. Kumar",
"course": "Bioinformatics",
"reviews": [
{
"rating": 4.2,
"review_text": "Dr. Kumar’s course on bioinformatics is well-structured and informative."
},
{
"rating": 4.0,
"review_text": "Interesting content, though the course could benefit from more practical exercises."
},
{
"rating": 4.5,
"review_text": "Excellent for understanding bioinformatics with practical insights and applications."
}
]
},
{
"professor_id": "I4J5K6",
"name": "Dr. S. Patel",
"course": "Human-Robot Interaction",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Patel’s course is engaging with a lot of hands-on projects and case studies."
},
{
"rating": 4.2,
"review_text": "Good theoretical content but could use more interactive sessions."
},
{
"rating": 4.7,
"review_text": "Highly recommended for those interested in robotics and human-robot interaction."
}
]
},
{
"professor_id": "J5K6L7",
"name": "Dr. N. Choudhury",
"course": "Advanced Databases",
"reviews": [
{
"rating": 4.6,
"review_text": "Dr. Choudhury offers a deep dive into advanced database systems with practical examples."
},
{
"rating": 4.3,
"review_text": "Theoretical aspects are well-covered but more real-world projects would be beneficial."
},
{
"rating": 4.8,
"review_text": "Excellent teaching with complex concepts made accessible through practical exercises."
}
]
},
{
"professor_id": "K6L7M8",
"name": "Dr. R. Bhatt",
"course": "Software Development Lifecycle",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Bhatt provides a solid understanding of the software development lifecycle with clear explanations."
},
{
"rating": 4.1,
"review_text": "Good course content but could use more interactive discussions and case studies."
},
{
"rating": 4.6,
"review_text": "Highly practical with useful insights into each phase of software development."
}
]
},
{
"professor_id": "L7M8N9",
"name": "Dr. A. Kapoor",
"course": "Data Mining",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Kapoor’s course is comprehensive with practical data mining techniques and tools."
},
{
"rating": 4.0,
"review_text": "Content is relevant but the pace could be adjusted for better learning."
},
{
"rating": 4.7,
"review_text": "Excellent for practical data mining applications with lots of hands-on exercises."
}
]
},
{
"professor_id": "M8N9O1",
"name": "Dr. T. Reddy",
"course": "Reinforcement Learning",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Reddy’s course offers a deep understanding of reinforcement learning with practical examples."
},
{
"rating": 4.2,
"review_text": "Good coverage of reinforcement learning concepts but could benefit from more interactive labs."
},
{
"rating": 4.8,
"review_text": "Highly engaging with excellent practical exercises and real-world applications."
}
]
},
{
"professor_id": "N9O1P2",
"name": "Dr. V. Sharma",
"course": "Augmented Reality",
"reviews": [
{
"rating": 4.4,
"review_text": "Dr. Sharma’s course is insightful with practical projects in augmented reality."
},
{
"rating": 4.1,
"review_text": "Interesting course but could use more hands-on exercises."
},
{
"rating": 4.6,
"review_text": "Excellent for learning about augmented reality with a lot of practical applications."
}
]
},
{
"professor_id": "O1P2Q3",
"name": "Dr. K. Mehta",
"course": "Blockchain Technology",
"reviews": [
{
"rating": 4.5,
"review_text": "Dr. Mehta provides a thorough understanding of blockchain technology with practical examples."
},
{
"rating": 4.2,
"review_text": "Good content but could benefit from more interactive labs."
},
{
"rating": 4.7,
"review_text": "Highly engaging with real-world applications and hands-on projects."
}
]
},
{
"professor_id": "P2Q3R4",
"name": "Dr. N. Patel",
"course": "Speech Processing",
"reviews": [
{
"rating": 4.3,
"review_text": "Dr. Patel’s course on speech processing is well-structured with practical examples."
},
{
"rating": 4.0,
"review_text": "Good theoretical coverage but could use more real-world applications."
},
{
"rating": 4.6,
"review_text": "Excellent course with a lot of practical exercises and applications in speech technology."
}
]
}
]
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import os
import json
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
from langchain_google_genai import ChatGoogleGenerativeAI
import google.generativeai as genai
from dotenv import load_dotenv
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
def extract_text(json_path):
with open(json_path, 'r') as file:
data = json.load(file)
text = ""
for professor in data['professors']:
professor_id = professor.get('professor_id')
name = professor.get('name')
course = professor.get('course')
reviews = professor.get('reviews', [])
text += f'\nProfessor ID: {professor_id}, Professor Name: {name}, Course: {course}\n '
if reviews:
for review in reviews:
rating = review.get('rating')
review_text = review.get('review_text')
text += f"Rating: {rating}, Review: {review_text}\n"
else:
print("No reviews available.")
return text
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("reviews_index")
def main(json_path):
text = extract_text(json_path)
chunks = split_text_into_chunks(text)
create_vector_store(chunks)
json_path = 'reviews.json'
main(json_path)main.pyAdded+118
reviews.jsonAdded+689
util.pyAdded+51