wholewhale commited on
Commit
26a8953
1 Parent(s): 6abee2b

Simple Anthropic

Browse files
Files changed (1) hide show
  1. app.py +36 -178
app.py CHANGED
@@ -1,193 +1,51 @@
1
  import gradio as gr
2
  import os
3
- import time
4
- import threading
5
  from langchain.document_loaders import OnlinePDFLoader
6
  from langchain.text_splitter import CharacterTextSplitter
7
- from langchain.llms import OpenAI
8
- from langchain.embeddings import OpenAIEmbeddings
9
- from langchain.vectorstores import Chroma
10
- from langchain.chains import ConversationalRetrievalChain
11
- from langchain.chat_models import ChatOpenAI
12
- from langchain.document_loaders import WebBaseLoader
13
- from langchain.chains.summarize import load_summarize_chain
14
- from langchain.chains.llm import LLMChain
15
- from langchain.prompts import PromptTemplate
16
- from langchain.chains.combine_documents.stuff import StuffDocumentsChain
17
 
18
- os.environ['OPENAI_API_KEY'] = os.getenv("Your_API_Key")
 
19
 
20
- # Global variable for tracking last interaction time
21
- last_interaction_time = 0
22
 
23
- def loading_pdf():
24
- return "Working on the upload. Also, pondering the usefulness of sporks..."
25
 
26
- # Inside Chroma mod
27
- def summary(self):
28
- num_documents = len(self.documents)
29
- avg_doc_length = sum(len(doc) for doc in self.documents) / num_documents
30
- return f"Number of documents: {num_documents}, Average document length: {avg_doc_length}"
31
-
32
- # Gradio state
33
- summary_state = gr.State(initial_value="")
34
-
35
- # Initialize loader and load documents
36
- def load_documents(pdf_doc):
37
- loader = OnlinePDFLoader(pdf_doc.name)
38
- return loader.load()
39
-
40
- # Generate summary using StuffDocumentsChain
41
- def generate_summary(documents):
42
- prompt_template = """Write a concise summary of the following:
43
- "{text}"
44
- CONCISE SUMMARY:"""
45
- prompt = PromptTemplate.from_template(prompt_template)
46
- llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
47
- llm_chain = LLMChain(llm=llm, prompt=prompt)
48
- stuff_chain = StuffDocumentsChain(
49
- llm_chain=llm_chain, document_variable_name="text"
50
- )
51
- return stuff_chain.run(documents)
52
-
53
- # Setup Chroma, embeddings, and retrieval
54
- def setup_retrieval(documents):
55
- embeddings = OpenAIEmbeddings()
56
- db = Chroma.from_documents(documents, embeddings)
57
- retriever = db.as_retriever()
58
- qa = ConversationalRetrievalChain.from_llm(
59
- llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo-16k", max_tokens=-1, n=2),
60
- retriever=retriever,
61
- return_source_documents=False
62
- )
63
- return db, qa
64
-
65
- # Main function to handle PDF changes
66
- def pdf_changes(pdf_doc):
67
  try:
68
- documents = load_documents(pdf_doc)
69
- global full_summary
70
- full_summary = generate_summary(documents)
71
- summary_state.value = full_summary # Update the state variable
72
- # ... (rest of your code)
73
- return f"Ready. Full Summary loaded."
74
- except Exception as e:
75
- return f"Error processing PDF: {str(e)}"
76
 
 
 
 
 
 
77
 
78
- def clear_data():
79
- global qa, db
80
- qa = None
81
- db = None
82
- return "Data cleared"
83
 
84
- def add_text(history, text):
85
- global last_interaction_time
86
- last_interaction_time = time.time()
87
- history = history + [(text, None)]
88
- return history, ""
89
-
90
- def bot(history):
91
- global full_summary
92
- if 'summary' in history[-1][0].lower(): # Check if the last question asks for a summary
93
- response = full_summary
94
- return full_summary
95
- else:
96
- response = infer(history[-1][0], history)
97
-
98
- sentences = ' \n'.join(response.split('. '))
99
- formatted_response = f"**Bot:**\n\n{sentences}"
100
- history[-1][1] = formatted_response
101
- return history
102
-
103
-
104
- def infer(question, history):
105
- try:
106
- res = []
107
- for human, ai in history[:-1]:
108
- pair = (human, ai)
109
- res.append(pair)
110
-
111
- chat_history = res
112
- query = question
113
- result = qa({"question": query, "chat_history": chat_history, "system": "This is a world-class summarizing AI, be helpful."})
114
- return result["answer"]
115
  except Exception as e:
116
- return f"Error querying chatbot: {str(e)}"
117
 
118
- def auto_clear_data():
119
- global qa, db, last_interaction_time
120
- if time.time() - last_interaction_time > 1000:
121
- qa = None
122
- db = None
123
- print("Data cleared successfully.") # Logging
124
 
125
- def periodic_clear():
126
- while True:
127
- auto_clear_data()
128
- time.sleep(1000)
129
-
130
- threading.Thread(target=periodic_clear).start()
131
-
132
- css = """
133
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
134
- """
135
-
136
- title = """
137
- <div style="text-align: center;max-width: 700px;">
138
- <h1>CauseWriter Chat with PDF OpenAI</h1>
139
- <p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
140
- when everything is ready, you can start asking questions about the pdf. Limit ~11k words. <br />
141
- This version is set to erase chat history automatically after page timeout and uses OpenAI.</p>
142
- </div>
143
- """
144
- # Global variable for tracking last interaction time
145
- last_interaction_time = 0
146
- full_summary = "" # Added global full_summary
147
-
148
- def update_summary_box():
149
- global full_summary
150
- return {"summary_box": full_summary}
151
-
152
- with gr.Blocks(css=css) as demo:
153
- with gr.Column(elem_id="col-container"):
154
- gr.HTML(title)
155
-
156
- with gr.Column():
157
- pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
158
- with gr.Row():
159
- langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
160
- load_pdf = gr.Button("Convert PDF to Magic AI language")
161
- clear_btn = gr.Button("Clear Data")
162
-
163
- # New Textbox to display summary
164
- summary_box = gr.Textbox(
165
- label="Document Summary",
166
- placeholder="Summary will appear here.",
167
- interactive=False,
168
- rows=5,
169
- elem_id="summary_box",
170
- state=summary_state # Bind the state here
171
- )
172
-
173
-
174
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=450)
175
- question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
176
- submit_btn = gr.Button("Send Message")
177
-
178
- load_pdf.click(loading_pdf, None, langchain_status)
179
- load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False).then(
180
- update_summary_box
181
- )
182
-
183
-
184
- # Then update the summary_box
185
- clear_btn.click(clear_data, outputs=[langchain_status], queue=False)
186
- question.submit(add_text, [chatbot, question], [chatbot, question]).then(
187
- bot, chatbot, chatbot
188
- )
189
- submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
190
- bot, chatbot, chatbot
191
- )
192
-
193
- demo.launch()
 
1
  import gradio as gr
2
  import os
 
 
3
  from langchain.document_loaders import OnlinePDFLoader
4
  from langchain.text_splitter import CharacterTextSplitter
5
+ from anthropic import LanguageModel
 
 
 
 
 
 
 
 
 
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):
16
+ global pdf_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
+ if pdf_doc is None:
19
+ return "No PDF uploaded."
 
 
 
 
 
 
20
 
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=100)
25
+ pdf_content = ' '.join(text_splitter.split_documents(documents))
26
 
27
+ return "PDF Loaded Successfully."
 
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
+ return f"Error processing PDF: {e}"
31
 
32
+ def chat_with_pdf(question):
33
+ context = [{"role": "system", "content": pdf_content}]
34
+ response = anthropic_model.query(question, context=context)
35
+ return response['answer']
 
 
36
 
37
+ # Define Gradio UI
38
+ def gradio_interface(pdf_doc, question):
39
+ if not pdf_content:
40
+ return load_pdf(pdf_doc)
41
+ else:
42
+ return chat_with_pdf(question)
43
+
44
+ gr.Interface(fn=gradio_interface,
45
+ inputs=[gr.File(label="Load a pdf", file_types=['.pdf'], type="file"),
46
+ gr.Textbox(label="Ask a question about the PDF")],
47
+ outputs="text",
48
+ live=True,
49
+ title="Chat with PDF content using Anthropic",
50
+ description="Upload a .PDF and interactively chat about its content."
51
+ ).launch()