Commit
·
39fc553
1
Parent(s):
26a8953
claude
Browse files
app.py
CHANGED
@@ -1,15 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
from langchain.document_loaders import OnlinePDFLoader
|
4 |
from langchain.text_splitter import CharacterTextSplitter
|
5 |
-
from
|
|
|
6 |
|
7 |
# Set API keys from environment variables
|
8 |
os.environ['ANTHROPIC_API_KEY'] = os.getenv("Your_Anthropic_API_Key")
|
9 |
|
10 |
-
# Initialize the Anthropic model
|
11 |
-
anthropic_model = LanguageModel(api_key=os.environ['ANTHROPIC_API_KEY'], model="some_model")
|
12 |
-
|
13 |
pdf_content = ""
|
14 |
|
15 |
def load_pdf(pdf_doc):
|
@@ -21,7 +18,7 @@ def load_pdf(pdf_doc):
|
|
21 |
# Load and split PDF content
|
22 |
loader = OnlinePDFLoader(pdf_doc.name)
|
23 |
documents = loader.load()
|
24 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=
|
25 |
pdf_content = ' '.join(text_splitter.split_documents(documents))
|
26 |
|
27 |
return "PDF Loaded Successfully."
|
@@ -30,9 +27,20 @@ def load_pdf(pdf_doc):
|
|
30 |
return f"Error processing PDF: {e}"
|
31 |
|
32 |
def chat_with_pdf(question):
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Define Gradio UI
|
38 |
def gradio_interface(pdf_doc, question):
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from langchain.document_loaders import OnlinePDFLoader
|
3 |
from langchain.text_splitter import CharacterTextSplitter
|
4 |
+
from langchain.chat_models import ChatAnthropic
|
5 |
+
from langchain.prompts import ChatPromptTemplate
|
6 |
|
7 |
# Set API keys from environment variables
|
8 |
os.environ['ANTHROPIC_API_KEY'] = os.getenv("Your_Anthropic_API_Key")
|
9 |
|
|
|
|
|
|
|
10 |
pdf_content = ""
|
11 |
|
12 |
def load_pdf(pdf_doc):
|
|
|
18 |
# Load and split PDF content
|
19 |
loader = OnlinePDFLoader(pdf_doc.name)
|
20 |
documents = loader.load()
|
21 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
22 |
pdf_content = ' '.join(text_splitter.split_documents(documents))
|
23 |
|
24 |
return "PDF Loaded Successfully."
|
|
|
27 |
return f"Error processing PDF: {e}"
|
28 |
|
29 |
def chat_with_pdf(question):
|
30 |
+
# Create an instance of the ChatAnthropic model
|
31 |
+
model = ChatAnthropic()
|
32 |
+
|
33 |
+
# Define the chat prompt template
|
34 |
+
prompt = ChatPromptTemplate.from_messages([
|
35 |
+
("human", pdf_content),
|
36 |
+
("human", question),
|
37 |
+
])
|
38 |
+
|
39 |
+
# Invoke the model using the chain
|
40 |
+
chain = prompt | model
|
41 |
+
response = chain.invoke({})
|
42 |
+
|
43 |
+
return response.content
|
44 |
|
45 |
# Define Gradio UI
|
46 |
def gradio_interface(pdf_doc, question):
|