aakash0563 commited on
Commit
fbffa21
1 Parent(s): 56d38bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import chromadb
3
+ from langchain.document_loaders import PyPDFLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from uuid import uuid4
6
+ import google.generativeai as genai
7
+ import re
8
+
9
+
10
+ # Necessary imports for Gradio
11
+
12
+ text_splitter = RecursiveCharacterTextSplitter(
13
+ chunk_size=800,
14
+ chunk_overlap=50
15
+ )
16
+ client = chromadb.PersistentClient("test")
17
+ collection = client.create_collection("test_data")
18
+
19
+ def upload_pdf(file_path):
20
+ loader = PyPDFLoader(file_path)
21
+ pages = loader.load()
22
+ documents = []
23
+ for page in pages:
24
+ docs = text_splitter.split_text(page.page_content)
25
+ for doc in docs:
26
+ documents.append({
27
+ "text": docs, "meta_data": page.metadata,
28
+ })
29
+ collection.add(
30
+ ids=[str(uuid4()) for _ in range(len(documents))],
31
+ documents=[doc['text'][0] for doc in documents],
32
+ metadatas=[doc['meta_data'] for doc in documents]
33
+ )
34
+ return f"PDF Uploaded Successfully. {collection.count()} chunks stored in ChromaDB"
35
+
36
+
37
+
38
+ model = genai.GenerativeModel('gemini-pro') # Load the model
39
+
40
+ def get_Answer(query):
41
+ res = collection.query( # Assuming `collection` is defined elsewhere
42
+ query_texts=query,
43
+ n_results=2
44
+ )
45
+ system = f"""You are a teacher. You will be provided some context, 
46
+ your task is to analyze the relevant context and answer the below question:
47
+ - {query}
48
+ """
49
+ context = " ".join([re.sub(r'[^\x00-\x7F]+', ' ', r) for r in res['documents'][0]])
50
+ prompt = f"### System: {system} \n\n ###: User: {context} \n\n ### Assistant:\n"
51
+ answer = model.generate_content(prompt).text
52
+ return answer
53
+
54
+ # # Define the Gradio interface
55
+ # iface = gr.Interface(
56
+ # fn=get_Answer,
57
+ # inputs=gr.Textbox(lines=5, placeholder="Ask a question"), # Textbox for query
58
+ # outputs="textbox", # Display the generated answer in a textbox
59
+ # title="Answer Questions with Gemini-Pro",
60
+ # description="Ask a question and get an answer based on context from a ChromaDB collection.",
61
+ # )
62
+
63
+ # # Launch the Gradio app
64
+ # iface.launch()
65
+
66
+
67
+ # # Define the Gradio interface
68
+ # iface = gr.Interface(
69
+ # fn=upload_pdf,
70
+ # inputs=["file"], # Specify a file input component
71
+ # outputs="textbox", # Display the output text in a textbox
72
+ # title="Upload PDF to ChromaDB",
73
+ # description="Upload a PDF file and store its text chunks in ChromaDB.",
74
+ # )
75
+
76
+ # # Launch the Gradio app
77
+ # iface.launch()