Ashish Ranjan Karn
commited on
Commit
·
055a3f7
1
Parent(s):
ec1e358
update
Browse files- app.py +84 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import os.path
|
3 |
+
import pymongo
|
4 |
+
from pymongo.mongo_client import MongoClient
|
5 |
+
import gradio as gr
|
6 |
+
import certifi
|
7 |
+
|
8 |
+
MONGODB_ATLAS_DB_PASSWORD = os.environ['MONGODB_ATLAS_DB_PASSWORD']
|
9 |
+
|
10 |
+
|
11 |
+
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
|
12 |
+
|
13 |
+
from llama_index import (
|
14 |
+
VectorStoreIndex,
|
15 |
+
SimpleDirectoryReader,
|
16 |
+
StorageContext,
|
17 |
+
load_index_from_storage,
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
mongo_uri = (
|
22 |
+
f"mongodb+srv://arkiitkgp:{MONGODB_ATLAS_DB_PASSWORD}@genaicluster0.fgmvvsx.mongodb.net/?retryWrites=true&w=majority"
|
23 |
+
)
|
24 |
+
mongodb_client = pymongo.MongoClient(mongo_uri, tlsCAFile=certifi.where())
|
25 |
+
|
26 |
+
# Send a ping to confirm a successful connection
|
27 |
+
try:
|
28 |
+
mongodb_client.admin.command('ping')
|
29 |
+
print("Pinged your deployment. You successfully connected to MongoDB!")
|
30 |
+
except Exception as e:
|
31 |
+
print(e)
|
32 |
+
|
33 |
+
|
34 |
+
store = MongoDBAtlasVectorSearch(mongodb_client, db_name='testdb1', collection_name='dummyIndex', index_name='vector_index')
|
35 |
+
# storage_context = StorageContext.from_defaults(vector_store=store)
|
36 |
+
# documents = SimpleDirectoryReader(
|
37 |
+
# "temp",
|
38 |
+
# file_metadata=get_metadata_from_filename,
|
39 |
+
# required_exts=['.pdf', '.docx'],
|
40 |
+
# recursive=True,
|
41 |
+
# ).load_data()
|
42 |
+
|
43 |
+
# construct index
|
44 |
+
# index = VectorStoreIndex.from_documents(
|
45 |
+
# documents, storage_context=storage_context
|
46 |
+
# )
|
47 |
+
|
48 |
+
index = VectorStoreIndex.from_vector_store(store)
|
49 |
+
|
50 |
+
|
51 |
+
def get_answer(query):
|
52 |
+
response = index.as_query_engine(streaming=True).query(query)
|
53 |
+
# response.print_response_stream()
|
54 |
+
return response
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
###### GRADIO #################################
|
59 |
+
|
60 |
+
classes = ['Class 10', 'Class 9']
|
61 |
+
subjects = ['Science']
|
62 |
+
|
63 |
+
def get_streaming_answer(input_query):
|
64 |
+
r = get_answer(input_query)
|
65 |
+
ans = ""
|
66 |
+
for new_tokens in r.response_gen:
|
67 |
+
ans += new_tokens
|
68 |
+
yield ans
|
69 |
+
|
70 |
+
with gr.Blocks() as demo:
|
71 |
+
gr.Markdown("""# NCERT Tutor \n ### Type your question...""")
|
72 |
+
with gr.Tab("For Students"):
|
73 |
+
choose_class = gr.Dropdown(label= "Class", choices=classes)
|
74 |
+
choose_subject = gr.Dropdown(label="Subject", choices=subjects)
|
75 |
+
question_input = gr.Textbox(label="Enter question...")
|
76 |
+
submit_button = gr.Button("Ask")
|
77 |
+
response_output = gr.Textbox(label="Answer...")
|
78 |
+
|
79 |
+
with gr.Tab("For Teachers"):
|
80 |
+
gr.Markdown("Coming soon...")
|
81 |
+
|
82 |
+
submit_button.click(fn=get_streaming_answer, inputs=question_input, outputs=response_output)
|
83 |
+
|
84 |
+
demo.launch(share=True, debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==1.11.1
|
2 |
+
llama-index==0.9.44
|
3 |
+
pypdf==4.0.1
|
4 |
+
gradio==4.17.0
|
5 |
+
pymongo[srv]==3.10
|