File size: 9,099 Bytes
cdeb7b2
1d6a862
 
cdeb7b2
1d6a862
81c9675
cdeb7b2
1d6a862
c5a8c72
add9a1c
 
 
 
f460321
c7e5683
 
 
807b3b1
 
 
 
 
6464518
 
de6817d
 
 
 
6464518
807b3b1
6464518
 
 
 
18809a3
 
 
de6817d
 
18809a3
 
de6817d
18809a3
 
aa23802
0cdd851
13469d1
 
 
de6817d
 
13469d1
 
18809a3
 
 
 
 
 
 
 
13469d1
 
66c6128
 
13469d1
66c6128
 
13469d1
66c6128
 
13469d1
66c6128
 
13469d1
66c6128
 
13469d1
5c6a6e5
 
16963ae
 
5c6a6e5
16963ae
 
13469d1
 
de392ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13469d1
81c9675
b40b15f
81c9675
 
b40b15f
81c9675
 
 
 
 
 
 
 
 
 
1d6a862
4bca50c
 
 
 
 
 
 
 
5c6a6e5
4bca50c
cdeb7b2
4bca50c
 
0d9856e
4bca50c
7acded7
81c9675
 
 
7acded7
81c9675
f460321
7acded7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81c9675
0d9856e
1d6a862
b40b15f
 
 
 
 
 
 
 
 
 
 
0d9856e
 
bd9d8d1
0d9856e
b40b15f
81c9675
 
 
b40b15f
81c9675
 
 
0d9856e
 
4204dae
0d9856e
cdeb7b2
81c9675
 
 
4204dae
81c9675
 
cdeb7b2
 
4bca50c
 
 
1d6a862
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
import gradio as gr
from gradio_client import Client, handle_file
import os

# Define your Hugging Face token (make sure to set it as an environment variable)
HF_TOKEN = os.getenv("HF_TOKEN")  # Replace with your actual token if not using an environment variable

# Initialize the Gradio Client for the specified API
client = Client("on1onmangoes/CNIHUB101324v10", hf_token=HF_TOKEN)

# Update the conversation history within the function.
# Return the updated history along with any other required outputs.

client_name = ['rosariarossi','bianchifiordaliso','lorenzoverdi','lucia', 'quarto4', 'quinto5', 'secondo6', 'sesto6', 'settimo7','ottavo8','nono9']



def stream_chat_with_rag(
    message: str,
    history: list,
    client_name: str,
    system_prompt: str,
    num_retrieved_docs: int = 10,
    num_docs_final: int = 9,
    temperature: float = 0, 
    max_new_tokens: int = 1024, 
    top_p: float = 1.0, 
    top_k: int = 20, 
    penalty: float = 1.2,
):
    print(f"Message: {message}")
    print(f"History: {history}")

    # Build the conversation prompt including system prompt and history
    conversation = f"{system_prompt}\n\nFor Client: {client_name}\n"
    
    # Add previous conversation history
    for user_input, assistant_response in history:
        conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
    
    # Add the current user message
    conversation += f"User: {message}\nAssistant:"

    # Call the API with the user's message
    question = message
    answer = client.predict(question=question, api_name="/answer_with_rag")

    # Debugging: Print the raw response
    print("Raw answer from API:")
    print(answer)

    # Format the assistant's answer and the relevant documents separately
    formatted_answer = format_answer_string(answer)

    # Update the conversation history with the new message and answer
    history.append((message, formatted_answer))

    # Return the formatted answer
    return formatted_answer


def format_answer_string(answer: str):
    """
    This function extracts and formats the assistant's response before document metadata.
    Anything after the marker `[(` (where documents are listed) is ignored.
    """
    # Step 1: Split the response at the start of the document metadata
    split_marker = "[("
    if split_marker in answer:
        # Everything before the marker is the relevant answer
        answer_before_docs = answer.split(split_marker)[0]
    else:
        # If no documents metadata, return the entire answer
        answer_before_docs = answer
    
    # Step 2: Clean up formatting by replacing escaped newline characters
    formatted_answer = answer_before_docs.replace("\\n", "\n").strip()
    
    # Step 3: Remove potential starting and ending artifacts like (' and ,) if present
    if formatted_answer.startswith("(\"") and formatted_answer.endswith("\","):
        formatted_answer = formatted_answer[2:-2].strip()

    # Optional: Add a prefix for clarity
    formatted_answer = "Co-Pilot: " + formatted_answer
    
    return formatted_answer

def format_relevant_documents(relevant_docs: list):
    """
    This function formats the relevant document metadata and content for readable output.
    It extracts the heading, page number, and a snippet of the content from each document.
    """
    formatted_docs = "Relevant Documents:\n\n"
    
    for idx, (doc, score) in enumerate(relevant_docs):
        # Extract the relevant metadata
        heading = doc.metadata.get('heading', 'Unnamed Document')
        page_number = int(doc.metadata.get('page_number', -1))
        source = doc.metadata.get('source', 'Unknown Source')
        confidence = round(score, 4)  # Rounding the score for cleaner output
        
        # Add the formatted details to the output string
        formatted_docs += f"Document {idx + 1}:\n"
        formatted_docs += f"  - Heading: {heading}\n"
        formatted_docs += f"  - Page Number: {page_number}\n"
        formatted_docs += f"  - Source: {source}\n"
        formatted_docs += f"  - Confidence Score: {confidence}\n"
        
        # Optionally include a snippet from the content
        content_snippet = doc.page_content[:200]  # Take the first 200 characters for preview
        formatted_docs += f"  - Content Snippet: {content_snippet}...\n\n"

    return formatted_docs.strip()


# Function to handle PDF processing API call
def process_pdf(pdf_file, client_name):
    return client.predict(
        pdf_file=handle_file(pdf_file),
        client_name=client_name,  # Hardcoded client name
        api_name="/process_pdf2"
    )[1]  # Return only the result string

# Function to handle search API call
def search_api(query):
    return client.predict(query=query, api_name="/search_with_confidence")

# Function to handle RAG API call
def rag_api(question):
    return client.predict(question=question, api_name="/answer_with_rag")

# CSS for custom styling
CSS = """
# chat-container {
    height: 100vh;
}
"""

# Title for the application
TITLE = "<h1 style='text-align:center;'>CNI RAG QA v0</h1>"

# Create the Gradio Blocks interface
with gr.Blocks(css=CSS) as demo:
    gr.HTML(TITLE)
    with gr.Tab("Chat"):
        chatbot = gr.Chatbot()  # Create a chatbot interface

        chat_interface = gr.ChatInterface(
            fn=stream_chat_with_rag,
            chatbot=chatbot,
            additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
            additional_inputs=[
                gr.Dropdown(client_name,value="rosariarossi",label="Select Client", render=False,allow_custom_value=True),
                gr.Textbox(
                    value="You are an expert assistant",
                    label="System Prompt",
                    render=False,
                ),
                gr.Slider(
                    minimum=1,
                    maximum=10,
                    step=1,
                    value=10,
                    label="Number of Initial Documents to Retrieve",
                    render=False,
                ),
                gr.Slider(
                    minimum=1,
                    maximum=10,
                    step=1,
                    value=9,
                    label="Number of Final Documents to Retrieve",
                    render=False,
                ),
                gr.Slider(
                    minimum=0.2,
                    maximum=1,
                    step=0.1,
                    value=0,
                    label="Temperature",
                    render=False,
                ),
                gr.Slider(
                    minimum=128,
                    maximum=8192,
                    step=1,
                    value=1024,
                    label="Max new tokens",
                    render=False,
                ),
                gr.Slider(
                    minimum=0.0,
                    maximum=1.0,
                    step=0.1,
                    value=1.0,
                    label="Top P",
                    render=False,
                ),
                gr.Slider(
                    minimum=1,
                    maximum=20,
                    step=1,
                    value=20,
                    label="Top K",
                    render=False,
                ),
                gr.Slider(
                    minimum=0.0,
                    maximum=2.0,
                    step=0.1,
                    value=1.2,
                    label="Repetition Penalty",
                    render=False,
                ),
            ],
        )

    # with gr.Tab("Process PDF"):
    #     pdf_input = gr.File(label="Upload PDF File")
    #     pdf_output = gr.Textbox(label="PDF Result", interactive=False)

    #     pdf_button = gr.Button("Process PDF")
    #     pdf_button.click(
    #         process_pdf,
    #         inputs=[pdf_input],
    #         outputs=pdf_output
    #     )

    with gr.Tab("Process PDF"):
        pdf_input = gr.File(label="Upload PDF File")
        select_client_dropdown = gr.Dropdown(client_name, value="rosariarossi", label="Select or Type Client", allow_custom_value=True)
        pdf_output = gr.Textbox(label="PDF Result", interactive=False)
    
        pdf_button = gr.Button("Process PDF")
        pdf_button.click(
            process_pdf,
            inputs=[pdf_input, select_client_dropdown],  # Pass both PDF and client name as inputs
            outputs=pdf_output
        )

    with gr.Tab("Answer with RAG"):
        question_input = gr.Textbox(label="Enter Question for RAG")
        answer_with_rag_select_client_dropdown = gr.Dropdown(client_name, value="rosariarossi", label="Select or Type Client", allow_custom_value=True)
        rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)

        rag_button = gr.Button("Get Answer")
        rag_button.click(
            rag_api,
            inputs=[question_input,answer_with_rag_select_client_dropdown ],
            outputs=rag_output
        )

# Launch the app
if __name__ == "__main__":
    demo.launch()