Spaces:
Running
Running
oceansweep
commited on
Commit
•
530424e
1
Parent(s):
6c74947
Upload 4 files
Browse files
App_Function_Libraries/Gradio_UI/Explain_summarize_tab.py
CHANGED
@@ -6,6 +6,9 @@ import logging
|
|
6 |
#
|
7 |
# External Imports
|
8 |
import gradio as gr
|
|
|
|
|
|
|
9 |
#
|
10 |
# Local Imports
|
11 |
from App_Function_Libraries.Local_Summarization_Lib import summarize_with_llama, summarize_with_kobold, \
|
@@ -25,35 +28,100 @@ def create_summarize_explain_tab():
|
|
25 |
gr.Markdown("# Explain or Summarize Text without ingesting it into the DB")
|
26 |
with gr.Row():
|
27 |
with gr.Column():
|
28 |
-
|
|
|
29 |
placeholder="Enter the text you want explained or summarized here",
|
30 |
lines=20)
|
31 |
with gr.Row():
|
32 |
explanation_checkbox = gr.Checkbox(label="Explain Text", value=True)
|
33 |
summarization_checkbox = gr.Checkbox(label="Summarize Text", value=True)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
with gr.Column():
|
46 |
summarization_output = gr.Textbox(label="Summary:", lines=20)
|
47 |
-
explanation_output = gr.Textbox(label="Explanation:", lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
explain_summarize_button.click(
|
50 |
fn=summarize_explain_text,
|
51 |
-
inputs=[text_to_work_input, api_endpoint, api_key_input, summarization_checkbox, explanation_checkbox],
|
52 |
-
outputs=[summarization_output, explanation_output]
|
53 |
)
|
54 |
|
55 |
|
56 |
-
def summarize_explain_text(message, api_endpoint, api_key, summarization, explanation):
|
|
|
57 |
summarization_response = None
|
58 |
explanation_response = None
|
59 |
temp = 0.7
|
@@ -176,6 +244,52 @@ def summarize_explain_text(message, api_endpoint, api_key, summarization, explan
|
|
176 |
logging.error(f"Error in summarization: {str(e)}")
|
177 |
response2 = f"An error occurred during summarization: {str(e)}"
|
178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
if summarization_response:
|
180 |
response1 = f"Summary: {summarization_response}"
|
181 |
else:
|
@@ -186,7 +300,12 @@ def summarize_explain_text(message, api_endpoint, api_key, summarization, explan
|
|
186 |
else:
|
187 |
response2 = "Explanation: No explanation requested"
|
188 |
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
except Exception as e:
|
192 |
logging.error(f"Error in chat function: {str(e)}")
|
|
|
6 |
#
|
7 |
# External Imports
|
8 |
import gradio as gr
|
9 |
+
|
10 |
+
from App_Function_Libraries.DB.DB_Manager import load_preset_prompts
|
11 |
+
from App_Function_Libraries.Gradio_UI.Gradio_Shared import update_user_prompt
|
12 |
#
|
13 |
# Local Imports
|
14 |
from App_Function_Libraries.Local_Summarization_Lib import summarize_with_llama, summarize_with_kobold, \
|
|
|
28 |
gr.Markdown("# Explain or Summarize Text without ingesting it into the DB")
|
29 |
with gr.Row():
|
30 |
with gr.Column():
|
31 |
+
with gr.Row():
|
32 |
+
text_to_work_input = gr.Textbox(label="Text to be Explained or Summarized",
|
33 |
placeholder="Enter the text you want explained or summarized here",
|
34 |
lines=20)
|
35 |
with gr.Row():
|
36 |
explanation_checkbox = gr.Checkbox(label="Explain Text", value=True)
|
37 |
summarization_checkbox = gr.Checkbox(label="Summarize Text", value=True)
|
38 |
+
custom_prompt_checkbox = gr.Checkbox(label="Use a Custom Prompt",
|
39 |
+
value=False,
|
40 |
+
visible=True)
|
41 |
+
preset_prompt_checkbox = gr.Checkbox(label="Use a pre-set Prompt",
|
42 |
+
value=False,
|
43 |
+
visible=True)
|
44 |
+
with gr.Row():
|
45 |
+
preset_prompt = gr.Dropdown(label="Select Preset Prompt",
|
46 |
+
choices=load_preset_prompts(),
|
47 |
+
visible=False)
|
48 |
+
with gr.Row():
|
49 |
+
custom_prompt_input = gr.Textbox(label="Custom Prompt",
|
50 |
+
placeholder="Enter custom prompt here",
|
51 |
+
lines=3,
|
52 |
+
visible=False)
|
53 |
+
with gr.Row():
|
54 |
+
system_prompt_input = gr.Textbox(label="System Prompt",
|
55 |
+
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]
|
56 |
+
**Bulleted Note Creation Guidelines**
|
57 |
+
|
58 |
+
**Headings**:
|
59 |
+
- Based on referenced topics, not categories like quotes or terms
|
60 |
+
- Surrounded by **bold** formatting
|
61 |
+
- Not listed as bullet points
|
62 |
+
- No space between headings and list items underneath
|
63 |
+
|
64 |
+
**Emphasis**:
|
65 |
+
- **Important terms** set in bold font
|
66 |
+
- **Text ending in a colon**: also bolded
|
67 |
+
|
68 |
+
**Review**:
|
69 |
+
- Ensure adherence to specified format
|
70 |
+
- Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]
|
71 |
+
""",
|
72 |
+
lines=3,
|
73 |
+
visible=False,
|
74 |
+
interactive=True)
|
75 |
+
api_endpoint = gr.Dropdown(
|
76 |
+
choices=[None, "Local-LLM", "OpenAI", "Anthropic", "Cohere", "Groq", "DeepSeek", "Mistral",
|
77 |
+
"OpenRouter",
|
78 |
+
"Llama.cpp", "Kobold", "Ooba", "Tabbyapi", "VLLM", "ollama", "HuggingFace"],
|
79 |
+
value=None,
|
80 |
+
label="API for Summarization (Optional)"
|
81 |
+
)
|
82 |
+
api_key_input = gr.Textbox(label="API Key (if required)", placeholder="Enter your API key here",
|
83 |
+
type="password")
|
84 |
+
with gr.Row():
|
85 |
+
explain_summarize_button = gr.Button("Explain/Summarize")
|
86 |
|
87 |
with gr.Column():
|
88 |
summarization_output = gr.Textbox(label="Summary:", lines=20)
|
89 |
+
explanation_output = gr.Textbox(label="Explanation:", lines=20)
|
90 |
+
custom_prompt_output = gr.Textbox(label="Custom Prompt:", lines=20, visible=True)
|
91 |
+
|
92 |
+
custom_prompt_checkbox.change(
|
93 |
+
fn=lambda x: (gr.update(visible=x), gr.update(visible=x)),
|
94 |
+
inputs=[custom_prompt_checkbox],
|
95 |
+
outputs=[custom_prompt_input, system_prompt_input]
|
96 |
+
)
|
97 |
+
preset_prompt_checkbox.change(
|
98 |
+
fn=lambda x: gr.update(visible=x),
|
99 |
+
inputs=[preset_prompt_checkbox],
|
100 |
+
outputs=[preset_prompt]
|
101 |
+
)
|
102 |
+
|
103 |
+
def update_prompts(preset_name):
|
104 |
+
prompts = update_user_prompt(preset_name)
|
105 |
+
return (
|
106 |
+
gr.update(value=prompts["user_prompt"], visible=True),
|
107 |
+
gr.update(value=prompts["system_prompt"], visible=True)
|
108 |
+
)
|
109 |
+
|
110 |
+
preset_prompt.change(
|
111 |
+
update_prompts,
|
112 |
+
inputs=preset_prompt,
|
113 |
+
outputs=[custom_prompt_input, system_prompt_input]
|
114 |
+
)
|
115 |
|
116 |
explain_summarize_button.click(
|
117 |
fn=summarize_explain_text,
|
118 |
+
inputs=[text_to_work_input, api_endpoint, api_key_input, summarization_checkbox, explanation_checkbox, custom_prompt_input, system_prompt_input],
|
119 |
+
outputs=[summarization_output, explanation_output, custom_prompt_output]
|
120 |
)
|
121 |
|
122 |
|
123 |
+
def summarize_explain_text(message, api_endpoint, api_key, summarization, explanation, custom_prompt, custom_system_prompt,):
|
124 |
+
global custom_prompt_output
|
125 |
summarization_response = None
|
126 |
explanation_response = None
|
127 |
temp = 0.7
|
|
|
244 |
logging.error(f"Error in summarization: {str(e)}")
|
245 |
response2 = f"An error occurred during summarization: {str(e)}"
|
246 |
|
247 |
+
try:
|
248 |
+
if custom_prompt:
|
249 |
+
system_prompt = custom_system_prompt
|
250 |
+
user_prompt = custom_prompt + input_data
|
251 |
+
# Use the existing API request code based on the selected endpoint
|
252 |
+
logging.info(f"Debug - Chat Function - API Endpoint: {api_endpoint}")
|
253 |
+
if api_endpoint.lower() == 'openai':
|
254 |
+
custom_prompt_output = summarize_with_openai(api_key, input_data, user_prompt, temp, system_prompt)
|
255 |
+
elif api_endpoint.lower() == "anthropic":
|
256 |
+
custom_prompt_output = summarize_with_anthropic(api_key, input_data, user_prompt, temp,
|
257 |
+
system_prompt)
|
258 |
+
elif api_endpoint.lower() == "cohere":
|
259 |
+
custom_prompt_output = summarize_with_cohere(api_key, input_data, user_prompt, temp, system_prompt)
|
260 |
+
elif api_endpoint.lower() == "groq":
|
261 |
+
custom_prompt_output = summarize_with_groq(api_key, input_data, user_prompt, temp, system_prompt)
|
262 |
+
elif api_endpoint.lower() == "openrouter":
|
263 |
+
custom_prompt_output = summarize_with_openrouter(api_key, input_data, user_prompt, temp,
|
264 |
+
system_prompt)
|
265 |
+
elif api_endpoint.lower() == "deepseek":
|
266 |
+
custom_prompt_output = summarize_with_deepseek(api_key, input_data, user_prompt, temp,
|
267 |
+
system_prompt)
|
268 |
+
elif api_endpoint.lower() == "llama.cpp":
|
269 |
+
custom_prompt_output = summarize_with_llama(input_data, user_prompt, temp, system_prompt)
|
270 |
+
elif api_endpoint.lower() == "kobold":
|
271 |
+
custom_prompt_output = summarize_with_kobold(input_data, api_key, user_prompt, temp, system_prompt)
|
272 |
+
elif api_endpoint.lower() == "ooba":
|
273 |
+
custom_prompt_output = summarize_with_oobabooga(input_data, api_key, user_prompt, temp,
|
274 |
+
system_prompt)
|
275 |
+
elif api_endpoint.lower() == "tabbyapi":
|
276 |
+
custom_prompt_output = summarize_with_tabbyapi(input_data, user_prompt, temp, system_prompt)
|
277 |
+
elif api_endpoint.lower() == "vllm":
|
278 |
+
custom_prompt_output = summarize_with_vllm(input_data, user_prompt, system_prompt)
|
279 |
+
elif api_endpoint.lower() == "local-llm":
|
280 |
+
custom_prompt_output = summarize_with_local_llm(input_data, user_prompt, temp, system_prompt)
|
281 |
+
elif api_endpoint.lower() == "huggingface":
|
282 |
+
custom_prompt_output = summarize_with_huggingface(api_key, input_data, user_prompt,
|
283 |
+
temp) # , system_prompt)
|
284 |
+
elif api_endpoint.lower() == "ollama":
|
285 |
+
custom_prompt_output = summarize_with_ollama(input_data, user_prompt, temp, system_prompt)
|
286 |
+
else:
|
287 |
+
raise ValueError(f"Unsupported API endpoint: {api_endpoint}")
|
288 |
+
except Exception as e:
|
289 |
+
logging.error(f"Error in summarization: {str(e)}")
|
290 |
+
response2 = f"An error occurred during summarization: {str(e)}"
|
291 |
+
|
292 |
+
|
293 |
if summarization_response:
|
294 |
response1 = f"Summary: {summarization_response}"
|
295 |
else:
|
|
|
300 |
else:
|
301 |
response2 = "Explanation: No explanation requested"
|
302 |
|
303 |
+
if custom_prompt_output:
|
304 |
+
response3 = f"Custom Prompt: {custom_prompt_output}"
|
305 |
+
else:
|
306 |
+
response3 = "Custom Prompt: No custom prompt requested"
|
307 |
+
|
308 |
+
return response1, response2, response3
|
309 |
|
310 |
except Exception as e:
|
311 |
logging.error(f"Error in chat function: {str(e)}")
|
App_Function_Libraries/Gradio_UI/Live_Recording.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Live_Recording.py
|
2 |
+
# Description: Gradio UI for live audio recording and transcription.
|
3 |
+
#
|
4 |
+
# Import necessary modules and functions
|
5 |
+
import os
|
6 |
+
# External Imports
|
7 |
+
import gradio as gr
|
8 |
+
# Local Imports
|
9 |
+
from App_Function_Libraries.Audio_Transcription_Lib import (record_audio, speech_to_text, save_audio_temp)
|
10 |
+
from App_Function_Libraries.DB.DB_Manager import add_media_to_database
|
11 |
+
#
|
12 |
+
#######################################################################################################################
|
13 |
+
#
|
14 |
+
# Functions:
|
15 |
+
|
16 |
+
whisper_models = ["small", "medium", "small.en", "medium.en", "medium", "large", "large-v1", "large-v2", "large-v3",
|
17 |
+
"distil-large-v2", "distil-medium.en", "distil-small.en"]
|
18 |
+
|
19 |
+
def create_live_recording_tab():
|
20 |
+
with gr.Tab("Live Recording and Transcription"):
|
21 |
+
gr.Markdown("# Live Audio Recording and Transcription")
|
22 |
+
with gr.Row():
|
23 |
+
with gr.Column():
|
24 |
+
duration = gr.Slider(minimum=1, maximum=8000, value=15, label="Recording Duration (seconds)")
|
25 |
+
whisper_models_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
|
26 |
+
vad_filter = gr.Checkbox(label="Use VAD Filter")
|
27 |
+
save_recording = gr.Checkbox(label="Save Recording")
|
28 |
+
save_to_db = gr.Checkbox(label="Save Transcription to Database")
|
29 |
+
custom_title = gr.Textbox(label="Custom Title (for database)", visible=False)
|
30 |
+
record_button = gr.Button("Record and Transcribe")
|
31 |
+
with gr.Column():
|
32 |
+
output = gr.Textbox(label="Transcription", lines=10)
|
33 |
+
audio_output = gr.Audio(label="Recorded Audio", visible=False)
|
34 |
+
|
35 |
+
def record_and_transcribe(duration, whisper_model, vad_filter, save_recording):
|
36 |
+
audio_data = record_audio(duration)
|
37 |
+
temp_file = save_audio_temp(audio_data)
|
38 |
+
segments = speech_to_text(temp_file, whisper_model=whisper_model, vad_filter=vad_filter)
|
39 |
+
transcription = "\n".join([segment["Text"] for segment in segments])
|
40 |
+
|
41 |
+
if save_recording:
|
42 |
+
return transcription, temp_file
|
43 |
+
else:
|
44 |
+
os.remove(temp_file)
|
45 |
+
return transcription, None
|
46 |
+
|
47 |
+
def save_transcription_to_db(transcription, custom_title):
|
48 |
+
if custom_title.strip() == "":
|
49 |
+
custom_title = "Self-recorded Audio"
|
50 |
+
|
51 |
+
add_media_to_database(
|
52 |
+
url="self_recorded",
|
53 |
+
info_dict={"title": custom_title, "uploader": "self-recorded"},
|
54 |
+
segments=[{"Text": transcription}],
|
55 |
+
summary="",
|
56 |
+
keywords="self-recorded,audio",
|
57 |
+
custom_prompt_input="",
|
58 |
+
whisper_model="self-recorded"
|
59 |
+
)
|
60 |
+
return "Transcription saved to database successfully."
|
61 |
+
|
62 |
+
def update_custom_title_visibility(save_to_db):
|
63 |
+
return gr.update(visible=save_to_db)
|
64 |
+
|
65 |
+
record_button.click(
|
66 |
+
fn=record_and_transcribe,
|
67 |
+
inputs=[duration, whisper_models_input, vad_filter, save_recording],
|
68 |
+
outputs=[output, audio_output]
|
69 |
+
)
|
70 |
+
|
71 |
+
save_to_db.change(
|
72 |
+
fn=update_custom_title_visibility,
|
73 |
+
inputs=[save_to_db],
|
74 |
+
outputs=[custom_title]
|
75 |
+
)
|
76 |
+
|
77 |
+
gr.Button("Save to Database").click(
|
78 |
+
fn=save_transcription_to_db,
|
79 |
+
inputs=[output, custom_title],
|
80 |
+
outputs=gr.Textbox(label="Database Save Status")
|
81 |
+
)
|
82 |
+
|
83 |
+
#
|
84 |
+
# End of Functions
|
85 |
+
########################################################################################################################
|