Spaces:
Runtime error
Runtime error
Commit ·
afad96b
1
Parent(s): b9ca8c8
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 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 |
import os
|
| 9 |
|
|
@@ -14,31 +11,6 @@ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
| 14 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 15 |
model = genai.GenerativeModel('gemini-pro') # Load the model
|
| 16 |
|
| 17 |
-
# Necessary imports for Gradio
|
| 18 |
-
|
| 19 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 20 |
-
chunk_size=800,
|
| 21 |
-
chunk_overlap=50
|
| 22 |
-
)
|
| 23 |
-
client = chromadb.PersistentClient("test")
|
| 24 |
-
collection = client.create_collection("test_data")
|
| 25 |
-
|
| 26 |
-
def upload_pdf(file_path):
|
| 27 |
-
loader = PyPDFLoader(file_path)
|
| 28 |
-
pages = loader.load()
|
| 29 |
-
documents = []
|
| 30 |
-
for page in pages:
|
| 31 |
-
docs = text_splitter.split_text(page.page_content)
|
| 32 |
-
for doc in docs:
|
| 33 |
-
documents.append({
|
| 34 |
-
"text": docs, "meta_data": page.metadata,
|
| 35 |
-
})
|
| 36 |
-
collection.add(
|
| 37 |
-
ids=[str(uuid4()) for _ in range(len(documents))],
|
| 38 |
-
documents=[doc['text'][0] for doc in documents],
|
| 39 |
-
metadatas=[doc['meta_data'] for doc in documents]
|
| 40 |
-
)
|
| 41 |
-
return f"PDF Uploaded Successfully. {collection.count()} chunks stored in ChromaDB"
|
| 42 |
|
| 43 |
|
| 44 |
|
|
@@ -58,27 +30,16 @@ def get_Answer(query):
|
|
| 58 |
answer = model.generate_content(prompt).text
|
| 59 |
return answer
|
| 60 |
|
| 61 |
-
# # Define the Gradio interface
|
| 62 |
-
# iface = gr.Interface(
|
| 63 |
-
# fn=get_Answer,
|
| 64 |
-
# inputs=gr.Textbox(lines=5, placeholder="Ask a question"), # Textbox for query
|
| 65 |
-
# outputs="textbox", # Display the generated answer in a textbox
|
| 66 |
-
# title="Answer Questions with Gemini-Pro",
|
| 67 |
-
# description="Ask a question and get an answer based on context from a ChromaDB collection.",
|
| 68 |
-
# )
|
| 69 |
-
|
| 70 |
-
# # Launch the Gradio app
|
| 71 |
-
# iface.launch(debug=True,share=True)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
# Define the Gradio interface
|
| 75 |
iface = gr.Interface(
|
| 76 |
-
fn=
|
| 77 |
-
inputs=
|
| 78 |
-
outputs="textbox", # Display the
|
| 79 |
-
title="
|
| 80 |
-
description="
|
| 81 |
)
|
| 82 |
|
| 83 |
# Launch the Gradio app
|
| 84 |
iface.launch(debug=True,share=True)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import upload_pdf
|
| 4 |
import re
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 11 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 12 |
model = genai.GenerativeModel('gemini-pro') # Load the model
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
|
|
|
|
| 30 |
answer = model.generate_content(prompt).text
|
| 31 |
return answer
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Define the Gradio interface
|
| 34 |
iface = gr.Interface(
|
| 35 |
+
fn=get_Answer,
|
| 36 |
+
inputs=gr.Textbox(lines=5, placeholder="Ask a question"), # Textbox for query
|
| 37 |
+
outputs="textbox", # Display the generated answer in a textbox
|
| 38 |
+
title="Answer Questions with Gemini-Pro",
|
| 39 |
+
description="Ask a question and get an answer based on context from a ChromaDB collection.",
|
| 40 |
)
|
| 41 |
|
| 42 |
# Launch the Gradio app
|
| 43 |
iface.launch(debug=True,share=True)
|
| 44 |
+
|
| 45 |
+
|