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