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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Explain_summarize_tab.py
# Gradio UI for explaining and summarizing text
#
# Imports
import logging
#
# External Imports
import gradio as gr

from App_Function_Libraries.DB.DB_Manager import list_prompts
from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_user_prompt
#
# Local Imports
from App_Function_Libraries.Summarization.Local_Summarization_Lib import summarize_with_llama, summarize_with_kobold, \
    summarize_with_oobabooga, summarize_with_tabbyapi, summarize_with_vllm, summarize_with_local_llm, \
    summarize_with_ollama
from App_Function_Libraries.Summarization.Summarization_General_Lib import summarize_with_openai, summarize_with_anthropic, \
    summarize_with_cohere, summarize_with_groq, summarize_with_openrouter, summarize_with_deepseek, \
    summarize_with_huggingface
from App_Function_Libraries.Utils.Utils import default_api_endpoint, global_api_endpoints, format_api_name


#
#
############################################################################################################
#
# Functions:

def create_summarize_explain_tab():
    try:
        default_value = None
        if default_api_endpoint:
            if default_api_endpoint in global_api_endpoints:
                default_value = format_api_name(default_api_endpoint)
            else:
                logging.warning(f"Default API endpoint '{default_api_endpoint}' not found in global_api_endpoints")
    except Exception as e:
        logging.error(f"Error setting default API endpoint: {str(e)}")
        default_value = None

    with gr.TabItem("Analyze Text", visible=True):
        gr.Markdown("# Analyze / Explain / Summarize Text without ingesting it into the DB")

        # Initialize state variables for pagination
        current_page_state = gr.State(value=1)
        total_pages_state = gr.State(value=1)

        with gr.Row():
            with gr.Column():
                with gr.Row():
                    text_to_work_input = gr.Textbox(
                        label="Text to be Explained or Summarized",
                        placeholder="Enter the text you want explained or summarized here",
                        lines=20
                    )
                with gr.Row():
                    explanation_checkbox = gr.Checkbox(label="Explain Text", value=True)
                    summarization_checkbox = gr.Checkbox(label="Summarize Text", value=True)
                    custom_prompt_checkbox = gr.Checkbox(
                        label="Use a Custom Prompt",
                        value=False,
                        visible=True
                    )
                    preset_prompt_checkbox = gr.Checkbox(
                        label="Use a pre-set Prompt",
                        value=False,
                        visible=True
                    )
                with gr.Row():
                    # Add pagination controls
                    preset_prompt = gr.Dropdown(
                        label="Select Preset Prompt",
                        choices=[],
                        visible=False
                    )
                    prev_page_button = gr.Button("Previous Page", visible=False)
                    page_display = gr.Markdown("Page 1 of X", visible=False)
                    next_page_button = gr.Button("Next Page", visible=False)
                with gr.Row():
                    custom_prompt_input = gr.Textbox(
                        label="Custom Prompt",
                        placeholder="Enter custom prompt here",
                        lines=10,
                        visible=False
                    )
                with gr.Row():
                    system_prompt_input = gr.Textbox(label="System Prompt",
                                                     value="""<s>You are a bulleted notes specialist. [INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]

                    **Bulleted Note Creation Guidelines**

        

                    **Headings**:

                    - Based on referenced topics, not categories like quotes or terms

                    - Surrounded by **bold** formatting 

                    - Not listed as bullet points

                    - No space between headings and list items underneath

        

                    **Emphasis**:

                    - **Important terms** set in bold font

                    - **Text ending in a colon**: also bolded

        

                    **Review**:

                    - Ensure adherence to specified format

                    - Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]

                    """,
                                                     lines=10,
                                                     visible=False,
                                                     interactive=True)
                    # Refactored API selection dropdown
                    api_endpoint = gr.Dropdown(
                        choices=["None"] + [format_api_name(api) for api in global_api_endpoints],
                        value=default_value,
                        label="API for Summarization/Analysis (Optional)"
                    )
                with gr.Row():
                    api_key_input = gr.Textbox(
                        label="API Key (if required)",
                        placeholder="Enter your API key here",
                        type="password"
                    )
                with gr.Row():
                    explain_summarize_button = gr.Button("Explain/Summarize")

            with gr.Column():
                summarization_output = gr.Textbox(label="Summary:", lines=20)
                explanation_output = gr.Textbox(label="Explanation:", lines=20)
                custom_prompt_output = gr.Textbox(label="Custom Prompt:", lines=20, visible=True)

        # Handle custom prompt checkbox change
        custom_prompt_checkbox.change(
            fn=lambda x: (gr.update(visible=x), gr.update(visible=x)),
            inputs=[custom_prompt_checkbox],
            outputs=[custom_prompt_input, system_prompt_input]
        )

        # Handle preset prompt checkbox change
        def on_preset_prompt_checkbox_change(is_checked):
            if is_checked:
                prompts, total_pages, current_page = list_prompts(page=1, per_page=20)
                page_display_text = f"Page {current_page} of {total_pages}"
                return (
                    gr.update(visible=True, interactive=True, choices=prompts),  # preset_prompt
                    gr.update(visible=True),  # prev_page_button
                    gr.update(visible=True),  # next_page_button
                    gr.update(value=page_display_text, visible=True),  # page_display
                    current_page,  # current_page_state
                    total_pages    # total_pages_state
                )
            else:
                return (
                    gr.update(visible=False, interactive=False),  # preset_prompt
                    gr.update(visible=False),  # prev_page_button
                    gr.update(visible=False),  # next_page_button
                    gr.update(visible=False),  # page_display
                    1,  # current_page_state
                    1   # total_pages_state
                )

        preset_prompt_checkbox.change(
            fn=on_preset_prompt_checkbox_change,
            inputs=[preset_prompt_checkbox],
            outputs=[
                preset_prompt,
                prev_page_button,
                next_page_button,
                page_display,
                current_page_state,
                total_pages_state
            ]
        )

        # Pagination button functions
        def on_prev_page_click(current_page, total_pages):
            new_page = max(current_page - 1, 1)
            prompts, total_pages, current_page = list_prompts(page=new_page, per_page=20)
            page_display_text = f"Page {current_page} of {total_pages}"
            return (
                gr.update(choices=prompts),
                gr.update(value=page_display_text),
                current_page
            )

        prev_page_button.click(
            fn=on_prev_page_click,
            inputs=[current_page_state, total_pages_state],
            outputs=[preset_prompt, page_display, current_page_state]
        )

        def on_next_page_click(current_page, total_pages):
            new_page = min(current_page + 1, total_pages)
            prompts, total_pages, current_page = list_prompts(page=new_page, per_page=20)
            page_display_text = f"Page {current_page} of {total_pages}"
            return (
                gr.update(choices=prompts),
                gr.update(value=page_display_text),
                current_page
            )

        next_page_button.click(
            fn=on_next_page_click,
            inputs=[current_page_state, total_pages_state],
            outputs=[preset_prompt, page_display, current_page_state]
        )

        # Update prompts when a preset is selected
        def update_prompts(preset_name):
            prompts = update_user_prompt(preset_name)
            return (
                gr.update(value=prompts["user_prompt"], visible=True),
                gr.update(value=prompts["system_prompt"], visible=True)
            )

        preset_prompt.change(
            fn=update_prompts,
            inputs=[preset_prompt],
            outputs=[custom_prompt_input, system_prompt_input]
        )

        explain_summarize_button.click(
            fn=summarize_explain_text,
            inputs=[
                text_to_work_input,
                api_endpoint,
                api_key_input,
                summarization_checkbox,
                explanation_checkbox,
                custom_prompt_input,
                system_prompt_input
            ],
            outputs=[summarization_output, explanation_output, custom_prompt_output]
        )



def summarize_explain_text(message, api_endpoint, api_key, summarization, explanation, custom_prompt, custom_system_prompt,):
    global custom_prompt_output
    summarization_response = None
    explanation_response = None
    temp = 0.7
    try:
        logging.info(f"Debug - summarize_explain_text Function - Message: {message}")
        logging.info(f"Debug - summarize_explain_text Function - API Endpoint: {api_endpoint}")

        # Prepare the input for the API
        input_data = f"User: {message}\n"
        # Print first 500 chars
        logging.info(f"Debug - Chat Function - Input Data: {input_data[:500]}...")
        logging.debug(f"Debug - Chat Function - API Key: {api_key[:10]}")
        user_prompt = " "
        if not api_endpoint:
            return "Please select an API endpoint", "Please select an API endpoint"
        try:
            if summarization:
                system_prompt = """<s>You are a bulleted notes specialist. [INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]

                **Bulleted Note Creation Guidelines**



                **Headings**:

                - Based on referenced topics, not categories like quotes or terms

                - Surrounded by **bold** formatting 

                - Not listed as bullet points

                - No space between headings and list items underneath



                **Emphasis**:

                - **Important terms** set in bold font

                - **Text ending in a colon**: also bolded



                **Review**:

                - Ensure adherence to specified format

                - Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]"""

                # Use the existing API request code based on the selected endpoint
                logging.info(f"Debug - Chat Function - API Endpoint: {api_endpoint}")
                if api_endpoint.lower() == 'openai':
                    summarization_response = summarize_with_openai(api_key, input_data, user_prompt, temp,
                                                                   system_prompt)
                elif api_endpoint.lower() == "anthropic":
                    summarization_response = summarize_with_anthropic(api_key, input_data, user_prompt, temp,
                                                                      system_prompt)
                elif api_endpoint.lower() == "cohere":
                    summarization_response = summarize_with_cohere(api_key, input_data, user_prompt, temp,
                                                                   system_prompt)
                elif api_endpoint.lower() == "groq":
                    summarization_response = summarize_with_groq(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "openrouter":
                    summarization_response = summarize_with_openrouter(api_key, input_data, user_prompt, temp,
                                                                       system_prompt)
                elif api_endpoint.lower() == "deepseek":
                    summarization_response = summarize_with_deepseek(api_key, input_data, user_prompt, temp,
                                                                     system_prompt)
                elif api_endpoint.lower() == "llama.cpp":
                    summarization_response = summarize_with_llama(input_data, user_prompt, api_key, temp, system_prompt)
                elif api_endpoint.lower() == "kobold":
                    summarization_response = summarize_with_kobold(input_data, api_key, user_prompt, temp,
                                                                   system_prompt)
                elif api_endpoint.lower() == "ooba":
                    summarization_response = summarize_with_oobabooga(input_data, api_key, user_prompt, temp,
                                                                      system_prompt)
                elif api_endpoint.lower() == "tabbyapi":
                    summarization_response = summarize_with_tabbyapi(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "vllm":
                    summarization_response = summarize_with_vllm(input_data, user_prompt, system_prompt)
                elif api_endpoint.lower() == "local-llm":
                    summarization_response = summarize_with_local_llm(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "huggingface":
                    summarization_response = summarize_with_huggingface(api_key, input_data, user_prompt,
                                                                        temp)  # , system_prompt)
                elif api_endpoint.lower() == "ollama":
                    summarization_response = summarize_with_ollama(input_data, user_prompt, None, api_key, temp, system_prompt)
                else:
                    raise ValueError(f"Unsupported API endpoint: {api_endpoint}")
        except Exception as e:
            logging.error(f"Error in summarization: {str(e)}")
            response1 = f"An error occurred during summarization: {str(e)}"

        try:
            if explanation:
                system_prompt = """You are a professional teacher. Please explain the content presented in an easy to digest fashion so that a non-specialist may understand it."""
                # Use the existing API request code based on the selected endpoint
                logging.info(f"Debug - Chat Function - API Endpoint: {api_endpoint}")
                if api_endpoint.lower() == 'openai':
                    explanation_response = summarize_with_openai(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "anthropic":
                    explanation_response = summarize_with_anthropic(api_key, input_data, user_prompt, temp,
                                                                    system_prompt)
                elif api_endpoint.lower() == "cohere":
                    explanation_response = summarize_with_cohere(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "groq":
                    explanation_response = summarize_with_groq(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "openrouter":
                    explanation_response = summarize_with_openrouter(api_key, input_data, user_prompt, temp,
                                                                     system_prompt)
                elif api_endpoint.lower() == "deepseek":
                    explanation_response = summarize_with_deepseek(api_key, input_data, user_prompt, temp,
                                                                   system_prompt)
                elif api_endpoint.lower() == "llama.cpp":
                    explanation_response = summarize_with_llama(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "kobold":
                    explanation_response = summarize_with_kobold(input_data, api_key, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "ooba":
                    explanation_response = summarize_with_oobabooga(input_data, api_key, user_prompt, temp,
                                                                    system_prompt)
                elif api_endpoint.lower() == "tabbyapi":
                    explanation_response = summarize_with_tabbyapi(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "vllm":
                    explanation_response = summarize_with_vllm(input_data, user_prompt, system_prompt)
                elif api_endpoint.lower() == "local-llm":
                    explanation_response = summarize_with_local_llm(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "huggingface":
                    explanation_response = summarize_with_huggingface(api_key, input_data, user_prompt,
                                                                      temp)  # , system_prompt)
                elif api_endpoint.lower() == "ollama":
                    explanation_response = summarize_with_ollama(input_data, user_prompt, temp, system_prompt)
                else:
                    raise ValueError(f"Unsupported API endpoint: {api_endpoint}")
        except Exception as e:
            logging.error(f"Error in summarization: {str(e)}")
            response2 = f"An error occurred during summarization: {str(e)}"

        try:
            if custom_prompt:
                system_prompt = custom_system_prompt
                user_prompt = custom_prompt + input_data
                # Use the existing API request code based on the selected endpoint
                logging.info(f"Debug - Chat Function - API Endpoint: {api_endpoint}")
                if api_endpoint.lower() == 'openai':
                    custom_prompt_output = summarize_with_openai(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "anthropic":
                    custom_prompt_output = summarize_with_anthropic(api_key, input_data, user_prompt, temp,
                                                                    system_prompt)
                elif api_endpoint.lower() == "cohere":
                    custom_prompt_output = summarize_with_cohere(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "groq":
                    custom_prompt_output = summarize_with_groq(api_key, input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "openrouter":
                    custom_prompt_output = summarize_with_openrouter(api_key, input_data, user_prompt, temp,
                                                                     system_prompt)
                elif api_endpoint.lower() == "deepseek":
                    custom_prompt_output = summarize_with_deepseek(api_key, input_data, user_prompt, temp,
                                                                   system_prompt)
                elif api_endpoint.lower() == "llama.cpp":
                    custom_prompt_output = summarize_with_llama(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "kobold":
                    custom_prompt_output = summarize_with_kobold(input_data, api_key, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "ooba":
                    custom_prompt_output = summarize_with_oobabooga(input_data, api_key, user_prompt, temp,
                                                                    system_prompt)
                elif api_endpoint.lower() == "tabbyapi":
                    custom_prompt_output = summarize_with_tabbyapi(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "vllm":
                    custom_prompt_output = summarize_with_vllm(input_data, user_prompt, system_prompt)
                elif api_endpoint.lower() == "local-llm":
                    custom_prompt_output = summarize_with_local_llm(input_data, user_prompt, temp, system_prompt)
                elif api_endpoint.lower() == "huggingface":
                    custom_prompt_output = summarize_with_huggingface(api_key, input_data, user_prompt,
                                                                      temp)  # , system_prompt)
                elif api_endpoint.lower() == "ollama":
                    custom_prompt_output = summarize_with_ollama(input_data, user_prompt, temp, system_prompt)
                else:
                    raise ValueError(f"Unsupported API endpoint: {api_endpoint}")
        except Exception as e:
            logging.error(f"Error in summarization: {str(e)}")
            response2 = f"An error occurred during summarization: {str(e)}"


        if summarization_response:
            response1 = f"Summary: {summarization_response}"
        else:
            response1 = "Summary: No summary requested"

        if explanation_response:
            response2 = f"Explanation: {explanation_response}"
        else:
            response2 = "Explanation: No explanation requested"

        if custom_prompt_output:
            response3 = f"Custom Prompt: {custom_prompt_output}"
        else:
            response3 = "Custom Prompt: No custom prompt requested"

        return response1, response2, response3

    except Exception as e:
        logging.error(f"Error in chat function: {str(e)}")
        return f"An error occurred: {str(e)}"