File size: 12,474 Bytes
94d3bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import base64
import requests
import gradio as gr
import PyPDF2
import google.generativeai as genai
from langchain.text_splitter import RecursiveCharacterTextSplitter
from sentence_transformers import SentenceTransformer, util
import numpy as np
import os
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.documents import Document

# Retrieve API keys from environment variables
google_api_key = os.getenv("GOOGLE_API_KEY")
tavily_api_key = os.getenv("TAVILY_API_KEY")
docusign_api_key = os.getenv("DOCUSIGN_API_KEY")

# Configure Google Generative AI
genai.configure(api_key=google_api_key)

# Create the Gemini model
generation_config = {
    "temperature": 0.7,
    "top_p": 0.95,
    "top_k": 64,
    "max_output_tokens": 65536,
    "response_mime_type": "text/plain",
}

model = genai.GenerativeModel(
    model_name="gemini-2.0-flash-thinking-exp-01-21",
    generation_config=generation_config,
)

chat_session = model.start_chat(history=[])

# Function to extract text from a PDF
def extract_text_from_pdf(file_path):
    try:
        with open(file_path, "rb") as file:
            reader = PyPDF2.PdfReader(file)
            text = "".join(page.extract_text() for page in reader.pages)
        return text
    except Exception as e:
        return f"Error extracting text from PDF: {e}"

# Function to chunk the text
def chunk_text(text, chunk_size=500, chunk_overlap=50):
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=len
    )
    chunks = text_splitter.split_text(text)
    return chunks

# Function to embed the chunks
def embed_chunks(chunks, model_name="all-MiniLM-L6-v2"):
    model = SentenceTransformer(model_name)
    embeddings = model.encode(chunks, convert_to_tensor=True)
    return embeddings, model

# Function to retrieve relevant chunks
def retrieve_relevant_chunks(query, chunks, embeddings, model, top_k=3):
    query_embedding = model.encode(query, convert_to_tensor=True)
    similarities = util.cos_sim(query_embedding, embeddings)[0]
    top_k = min(top_k, len(chunks))
    top_indices = np.argsort(similarities.cpu().numpy())[-top_k:][::-1]
    relevant_chunks = [chunks[i] for i in top_indices]
    return relevant_chunks

# Function to summarize the agreement using Gemini
def summarize_agreement_with_gemini(text):
    try:
        # Create a prompt for summarization
        prompt = f"Summarize the following text in 3-5 sentences:\n\n{text}\n\nSummary:"
        
        # Send the prompt to the Gemini model
        response = chat_session.send_message(prompt)
        
        return response.text
    except Exception as e:
        return f"Error summarizing text with Gemini: {e}"

# Configure Tavily API
os.environ["TAVILY_API_KEY"] = tavily_api_key
web_search_tool = TavilySearchResults(k=3)

def generate_response_with_rag(query, pdf_path, state):
    if "chunks" not in state or "embeddings" not in state or "embedding_model" not in state:
        text = extract_text_from_pdf(pdf_path)
        chunks = chunk_text(text)
        embeddings, embedding_model = embed_chunks(chunks)
        state["chunks"] = chunks
        state["embeddings"] = embeddings
        state["embedding_model"] = embedding_model
    else:
        chunks = state["chunks"]
        embeddings = state["embeddings"]
        embedding_model = state["embedding_model"]

    # Retrieve relevant chunks based on the query
    relevant_chunks = retrieve_relevant_chunks(query, chunks, embeddings, embedding_model, top_k=5)  # Increase top_k
    
    # Debug: Print relevant chunks
    print(f"Relevant Chunks: {relevant_chunks}")

    # Combine the relevant chunks into a single context
    context = "\n\n".join(relevant_chunks)

    # Debug: Print the context
    print(f"Context from PDF: {context}")

    # Create a prompt that instructs the model to answer only from the context
    prompt = f"""
    You are a helpful assistant that answers questions based on the provided context. 
    Use the context below to answer the question. If the context does not contain enough information to answer the question, respond with "I don't know."

    **Context:**
    {context}

    **Question:**
    {query}

    **Answer:**
    """

    # Debug: Print the prompt
    print(f"Prompt for Gemini: {prompt}")

    # Send the prompt to the Gemini model
    try:
        response = chat_session.send_message(prompt)
        initial_answer = response.text

        # Check if the initial answer is "I don't know"
        if "I don't know" in initial_answer or "i don't know" in initial_answer:
            print("Initial answer is 'I don't know'. Performing web search...")
            docs = web_search_tool.invoke({"query": query})
            web_results = "\n".join([d["content"] for d in docs])
            web_results = Document(page_content=web_results)
            
            # Debug: Print web search results
            print(f"Web Search Results: {web_results.page_content}")

            # Create a prompt that instructs the model to answer from the web search results
            web_prompt = f"""
            You are a helpful assistant that answers questions based on the provided context. 
            The context below is from a web search. Use the context to answer the question. If the context does not contain enough information to answer the question, respond with "I don't know."
            
            **Context:**
            {web_results.page_content}

            **Question:**
            {query}

            **Answer:**
            """
            
            # Debug: Print the prompt
            print(f"Prompt for Gemini (Web Search): {web_prompt}")

            # Send the prompt to the Gemini model
            web_response = chat_session.send_message(web_prompt)
            # Add a note indicating the answer is based on a web search
            return f"{web_response.text}\n\n*Note: This answer is based on a web search.*"
        else:
            return initial_answer
    except Exception as e:
        return f"Error generating response: {e}"

# Function to send document to DocuSign
def send_to_docusign(file_path, recipient_email, recipient_name):
    account_id = "184d0409-2626-4c48-98b5-d383b9854a47"
    base_url = "https://demo.docusign.net/restapi"

    with open(file_path, "rb") as file:
        document_base64 = base64.b64encode(file.read()).decode()

    envelope_definition = {
        "emailSubject": "Please sign this document",
        "documents": [
            {
                "documentId": "1",
                "name": "document.pdf",
                "fileExtension": "pdf",
                "documentBase64": document_base64
            }
        ],
        "recipients": {
            "signers": [
                {
                    "email": recipient_email,
                    "name": recipient_name,
                    "recipientId": "1",
                    "tabs": {
                        "signHereTabs": [
                            {
                                "documentId": "1",
                                "pageNumber": "1",
                                "xPosition": "100",
                                "yPosition": "100"
                            }
                        ]
                    }
                }
            ]
        },
        "status": "sent"
    }

    headers = {
        "Authorization": f"Bearer {docusign_api_key}",
        "Content-Type": "application/json"
    }
    try:
        response = requests.post(
            f"{base_url}/v2.1/accounts/{account_id}/envelopes",
            headers=headers,
            json=envelope_definition
        )
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return {"error": str(e)}

# Function to process the agreement
def process_agreement(file, recipient_email, recipient_name, state):
    try:
        text = extract_text_from_pdf(file.name)
        if text.startswith("Error"):
            return text, {}, {}, state

        # Use Gemini for summarization
        summary = summarize_agreement_with_gemini(text)
        if summary.startswith("Error"):
            return summary, {}, {}, state

        docusign_response = send_to_docusign(file.name, recipient_email, recipient_name)
        if "error" in docusign_response:
            return summary, {}, docusign_response, state

        return summary, {}, docusign_response, state
    except Exception as e:
        return f"Error: {e}", {}, {}, state

# Gradio interface
def main_interface(file, recipient_email, recipient_name, question, state):
    if file is not None:
        state["file"] = file
        state["text"] = extract_text_from_pdf(file.name)
        state["chat_history"] = []  # Initialize chat history

    summary_output = ""
    docusign_output = {}
    chatbot_output = ""

    if "file" in state:
        if recipient_email and recipient_name:
            summary_output, _, docusign_output, state = process_agreement(state["file"], recipient_email, recipient_name, state)

        if question:
            chatbot_output = generate_response_with_rag(question, state["file"].name, state)
            state["chat_history"].append((question, chatbot_output))  # Update chat history

    return summary_output, docusign_output, chatbot_output, state

# CSS for styling
css = """
.gradio-container {
    background-image: url('https://huggingface.co/spaces/Nadaazakaria/DocWise/resolve/main/DALL%C2%B7E%202025-01-26%2011.43.33%20-%20A%20futuristic%20and%20sleek%20magical%20animated%20GIF-style%20icon%20design%20for%20%27DocWise%27%2C%20representing%20knowledge%2C%20documents%2C%20and%20wisdom.%20The%20design%20includes%20a%20glow.jpg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.gradio-container h1, 
.gradio-container .tabs > .tab-nav > .tab-button {
    color: #FFF5E1 !important;
    text-shadow: 0 0 5px rgba(255, 245, 225, 0.5);
}

.tabs {
    background-color: #f0f0f0 !important;
    border-radius: 10px !important;
    padding: 20px !important;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
}

.tabs > .tab-nav {
    background-color: #e0e0e0 !important;
    border-radius: 5px !important;
    margin-bottom: 15px !important;
}

.tabs > .tab-nav > .tab-button {
    color: black !important;
    font-weight: bold !important;
}

.tabs > .tab-nav > .tab-button.selected {
    background-color: #d0d0d0 !important;
    color: black !important;
}

#process-button, #chatbot-button {
    background-color: white !important;
    color: black !important;
    border: 1px solid #ccc !important;
    padding: 10px 20px !important;
    border-radius: 5px !important;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
    transition: background-color 0.3s ease !important;
}

#process-button:hover, #chatbot-button:hover {
    background-color: #f0f0f0 !important;
}
"""

# Gradio app
with gr.Blocks(css=css) as app:
    gr.Markdown(
        """
    <div style="text-align: center;">
        <h1 id="main-title">
            DocWise(Agreement Analyzer with Chatbot and Docusign Integration)
        </h1>
    </div>
        """,
    )

    state = gr.State({})
    file_input = gr.File(label="Upload Agreement (PDF)")

    with gr.Tab("Agreement Processing", elem_id="agreement-tab"):
        email_input = gr.Textbox(label="Recipient Email")
        name_input = gr.Textbox(label="Recipient Name")
        summary_output = gr.Textbox(label="Agreement Summary")
        docusign_output = gr.JSON(label="DocuSign Response")
        process_button = gr.Button("Process Agreement", elem_id="process-button")

    with gr.Tab("Chatbot", elem_id="chatbot-tab"):
        chatbot_question_input = gr.Textbox(label="Ask a Question")
        chatbot_answer_output = gr.Textbox(label="Answer")
        chatbot_button = gr.Button("Ask", elem_id="chatbot-button")

    process_button.click(
        main_interface,
        inputs=[file_input, email_input, name_input, chatbot_question_input, state],
        outputs=[summary_output, docusign_output, chatbot_answer_output, state]
    )
    chatbot_button.click(
        main_interface,
        inputs=[file_input, email_input, name_input, chatbot_question_input, state],
        outputs=[summary_output, docusign_output, chatbot_answer_output, state]
    )

app.launch(debug=True)