Spaces:
Running
Running
oceansweep
commited on
Commit
•
a01c107
1
Parent(s):
e3cd24c
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,7 @@ import unicodedata
|
|
14 |
import zipfile
|
15 |
|
16 |
import gradio as gr
|
|
|
17 |
import torch
|
18 |
import yt_dlp
|
19 |
|
@@ -180,6 +181,7 @@ print(r"""
|
|
180 |
# Perform Platform Check
|
181 |
userOS = ""
|
182 |
|
|
|
183 |
|
184 |
def platform_check():
|
185 |
global userOS
|
@@ -388,16 +390,16 @@ def process_url(url, num_speakers, whisper_model, custom_prompt, offset, api_nam
|
|
388 |
|
389 |
if summary_file_path and os.path.exists(summary_file_path):
|
390 |
return formatted_transcription, summary_text, prettified_json_file_path, summary_file_path, video_file_path, None
|
|
|
|
|
391 |
else:
|
392 |
-
return formatted_transcription,
|
393 |
else:
|
394 |
return "No results found.", "Summary not available", None, None, None, None
|
395 |
except Exception as e:
|
396 |
return str(e), "Error processing the request.", None, None, None, None
|
397 |
|
398 |
|
399 |
-
|
400 |
-
|
401 |
def create_download_directory(title):
|
402 |
base_dir = "Results"
|
403 |
# Remove characters that are illegal in Windows filenames and normalize
|
@@ -439,7 +441,6 @@ def get_youtube(video_url):
|
|
439 |
return None
|
440 |
|
441 |
|
442 |
-
|
443 |
def get_playlist_videos(playlist_url):
|
444 |
ydl_opts = {
|
445 |
'extract_flat': True,
|
@@ -826,6 +827,7 @@ def summarize_with_openai(api_key, file_path, model, custom_prompt):
|
|
826 |
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
827 |
|
828 |
if response.status_code == 200:
|
|
|
829 |
summary = response.json()['choices'][0]['message']['content'].strip()
|
830 |
logging.debug("openai: Summarization successful")
|
831 |
print("Summarization successful.")
|
@@ -885,6 +887,7 @@ def summarize_with_claude(api_key, file_path, model, custom_prompt):
|
|
885 |
logging.debug("anthropic: Post submittal successful")
|
886 |
response_data = response.json()
|
887 |
try:
|
|
|
888 |
summary = response_data['content'][0]['text'].strip()
|
889 |
logging.debug("anthropic: Summarization successful")
|
890 |
print("Summary processed successfully.")
|
@@ -945,8 +948,9 @@ def summarize_with_cohere(api_key, file_path, model, custom_prompt):
|
|
945 |
|
946 |
if response.status_code == 200:
|
947 |
if 'text' in response_data:
|
|
|
948 |
summary = response_data['text'].strip()
|
949 |
-
logging.debug("cohere: Summarization successful")
|
950 |
print("Summary processed successfully.")
|
951 |
return summary
|
952 |
else:
|
@@ -999,6 +1003,7 @@ def summarize_with_groq(api_key, file_path, model, custom_prompt):
|
|
999 |
|
1000 |
if response.status_code == 200:
|
1001 |
if 'choices' in response_data and len(response_data['choices']) > 0:
|
|
|
1002 |
summary = response_data['choices'][0]['message']['content'].strip()
|
1003 |
logging.debug("groq: Summarization successful")
|
1004 |
print("Summarization successful.")
|
@@ -1051,6 +1056,7 @@ def summarize_with_llama(api_url, file_path, token, custom_prompt):
|
|
1051 |
if response.status_code == 200:
|
1052 |
# if 'X' in response_data:
|
1053 |
logging.debug(response_data)
|
|
|
1054 |
summary = response_data['content'].strip()
|
1055 |
logging.debug("llama: Summarization successful")
|
1056 |
print("Summarization successful.")
|
@@ -1100,6 +1106,7 @@ def summarize_with_kobold(kobold_ip, json_file_path, kobold_token, custom_prompt
|
|
1100 |
|
1101 |
if response.status_code == 200:
|
1102 |
if 'results' in response_data and len(response_data['results']) > 0:
|
|
|
1103 |
summary = response_data['results'][0]['text'].strip()
|
1104 |
logging.debug("kobold: Summarization successful")
|
1105 |
print("Summarization successful.")
|
@@ -1152,6 +1159,7 @@ def summarize_with_oobabooga(ooba_ip, json_file_path, ooba_token, custom_prompt)
|
|
1152 |
|
1153 |
if response.status_code == 200:
|
1154 |
response_data = response.json()
|
|
|
1155 |
summary = response.json()['choices'][0]['message']['content']
|
1156 |
logging.debug("ooba: Summarization successful")
|
1157 |
print("Summarization successful.")
|
@@ -1185,35 +1193,22 @@ def save_summary_to_file(summary, file_path):
|
|
1185 |
# Only to be used when configured with Gradio for HF Space
|
1186 |
def summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt):
|
1187 |
logging.debug(f"huggingface: Summarization process starting...")
|
|
|
1188 |
|
1189 |
-
model = "microsoft/Phi-3-mini-128k-instruct"
|
|
|
1190 |
API_URL = f"https://api-inference.huggingface.co/models/{model}"
|
1191 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
1192 |
|
|
|
|
|
|
|
|
|
1193 |
with open(json_file_path, 'r') as file:
|
1194 |
segments = json.load(file)
|
1195 |
text = ''.join([segment['text'] for segment in segments])
|
1196 |
|
1197 |
-
|
1198 |
-
data = {
|
1199 |
-
"inputs": text + "\n\n\n\n" + custom_prompt,
|
1200 |
-
"parameters": {"max_length": 4096, "min_length": 100}
|
1201 |
-
}
|
1202 |
-
|
1203 |
-
max_retries = 5
|
1204 |
-
|
1205 |
-
for attempt in range(max_retries):
|
1206 |
-
response = requests.post(API_URL, headers=headers, json=data)
|
1207 |
-
if response.status_code == 200:
|
1208 |
-
summary = response.json()[0]['summary_text']
|
1209 |
-
return summary, None
|
1210 |
-
elif response.status_code == 503:
|
1211 |
-
response_data = response.json()
|
1212 |
-
wait_time = response_data.get('estimated_time', 10)
|
1213 |
-
return None, f"Model is loading, retrying in {int(wait_time)} seconds..."
|
1214 |
-
# FIXME : This is a hack, should be done better
|
1215 |
-
# Sleep before retrying....
|
1216 |
-
# time.sleep(wait_time)
|
1217 |
|
1218 |
if huggingface_api_key == "":
|
1219 |
api_key = os.getenv(HF_TOKEN)
|
@@ -1230,17 +1225,22 @@ def summarize_with_huggingface(huggingface_api_key, json_file_path, custom_promp
|
|
1230 |
logging.debug("HUGGINGFACE API KEY CHECK #2: " + huggingface_api_key)
|
1231 |
|
1232 |
logging.debug("huggingface: Submitting request...")
|
1233 |
-
|
1234 |
-
response
|
1235 |
-
|
1236 |
-
if response
|
1237 |
-
|
1238 |
-
|
1239 |
-
|
1240 |
-
|
|
|
|
|
|
|
|
|
|
|
1241 |
else:
|
1242 |
-
logging.error(f"huggingface: Summarization failed with status code {response
|
1243 |
-
return f"Failed to process summary,
|
1244 |
except Exception as e:
|
1245 |
logging.error("huggingface: Error in processing: %s", str(e))
|
1246 |
print(f"Error occurred while processing summary with huggingface: {str(e)}")
|
@@ -1274,6 +1274,7 @@ def update_visibility(mode):
|
|
1274 |
# Hide all inputs below URL
|
1275 |
return [gr.update(visible=False)] * 9
|
1276 |
|
|
|
1277 |
# https://www.gradio.app/guides/controlling-layout
|
1278 |
def launch_ui(demo_mode=False):
|
1279 |
whisper_models = ["small.en", "medium.en", "large"]
|
@@ -1307,7 +1308,8 @@ def launch_ui(demo_mode=False):
|
|
1307 |
offset_input = gr.Number(value=0, label="Offset (Seconds into the video to start transcribing at)",
|
1308 |
visible=False)
|
1309 |
api_name_input = gr.Dropdown(
|
1310 |
-
choices=[None,"huggingface", "openai", "anthropic", "cohere", "groq", "llama", "kobold", "ooba"],
|
|
|
1311 |
label="API Name (Mandatory Unless you just want a Transcription)", visible=True)
|
1312 |
api_key_input = gr.Textbox(label="API Key (Mandatory if API Name is specified)",
|
1313 |
placeholder="Enter your API key here", visible=True)
|
@@ -1370,6 +1372,7 @@ def launch_ui(demo_mode=False):
|
|
1370 |
|
1371 |
iface.launch(share=False)
|
1372 |
|
|
|
1373 |
#
|
1374 |
#
|
1375 |
#####################################################################################################################################
|
@@ -1442,18 +1445,33 @@ def main(input_path, api_name=None, api_key=None, num_speakers=2, whisper_model=
|
|
1442 |
logging.info(f"Transcription complete: {audio_file}")
|
1443 |
|
1444 |
# Perform summarization based on the specified API
|
1445 |
-
logging.debug(f"MAIN:
|
1446 |
json_file_path = audio_file.replace('.wav', '.segments.json')
|
1447 |
if api_name == "huggingface":
|
1448 |
huggingface_api_key = os.getenv('HF_TOKEN').replace('"', '')
|
1449 |
if huggingface_api_key is None:
|
1450 |
-
huggingface_api_key = api_key if api_key else config.get('API', 'huggingface_api_key',
|
|
|
1451 |
try:
|
1452 |
logging.debug(f"MAIN: Trying to summarize with huggingface")
|
1453 |
summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt)
|
1454 |
except requests.exceptions.ConnectionError:
|
1455 |
requests.status_code = "Connection: "
|
1456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1457 |
logging.debug(f"MAIN: Summarization being performed by {api_name}")
|
1458 |
json_file_path = audio_file.replace('.wav', '.segments.json')
|
1459 |
if api_name.lower() == 'openai':
|
@@ -1466,17 +1484,20 @@ def main(input_path, api_name=None, api_key=None, num_speakers=2, whisper_model=
|
|
1466 |
elif api_name.lower() == "huggingface":
|
1467 |
huggingface_api_key = os.getenv(HF_TOKEN)
|
1468 |
if huggingface_api_key is None:
|
1469 |
-
huggingface_api_key = api_key if api_key else config.get('API', 'huggingface_api_key',
|
|
|
1470 |
try:
|
1471 |
logging.debug(f"MAIN: Trying to summarize with huggingface")
|
1472 |
summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt)
|
1473 |
except requests.exceptions.ConnectionError:
|
1474 |
requests.status_code = "Connection: "
|
1475 |
elif api_name.lower() == "anthropic":
|
1476 |
-
anthropic_api_key = api_key if api_key else config.get('API', 'anthropic_api_key',
|
|
|
1477 |
try:
|
1478 |
logging.debug(f"MAIN: Trying to summarize with anthropic")
|
1479 |
-
summary = summarize_with_claude(anthropic_api_key, json_file_path, anthropic_model,
|
|
|
1480 |
except requests.exceptions.ConnectionError:
|
1481 |
requests.status_code = "Connection: "
|
1482 |
elif api_name.lower() == "cohere":
|
@@ -1521,6 +1542,7 @@ def main(input_path, api_name=None, api_key=None, num_speakers=2, whisper_model=
|
|
1521 |
logging.warning(f"Unsupported API: {api_name}")
|
1522 |
summary = None
|
1523 |
|
|
|
1524 |
if summary:
|
1525 |
transcription_result['summary'] = summary
|
1526 |
logging.info(f"Summary generated using {api_name} API")
|
|
|
14 |
import zipfile
|
15 |
|
16 |
import gradio as gr
|
17 |
+
from huggingface_hub import InferenceClient
|
18 |
import torch
|
19 |
import yt_dlp
|
20 |
|
|
|
181 |
# Perform Platform Check
|
182 |
userOS = ""
|
183 |
|
184 |
+
global summary
|
185 |
|
186 |
def platform_check():
|
187 |
global userOS
|
|
|
390 |
|
391 |
if summary_file_path and os.path.exists(summary_file_path):
|
392 |
return formatted_transcription, summary_text, prettified_json_file_path, summary_file_path, video_file_path, None
|
393 |
+
#elif api_name.lower() == 'huggingface':
|
394 |
+
# return formatted_transcription, waiting_summary, prettified_json_file_path, None, video_file_path, None
|
395 |
else:
|
396 |
+
return formatted_transcription, summary_text, prettified_json_file_path, None, video_file_path, None
|
397 |
else:
|
398 |
return "No results found.", "Summary not available", None, None, None, None
|
399 |
except Exception as e:
|
400 |
return str(e), "Error processing the request.", None, None, None, None
|
401 |
|
402 |
|
|
|
|
|
403 |
def create_download_directory(title):
|
404 |
base_dir = "Results"
|
405 |
# Remove characters that are illegal in Windows filenames and normalize
|
|
|
441 |
return None
|
442 |
|
443 |
|
|
|
444 |
def get_playlist_videos(playlist_url):
|
445 |
ydl_opts = {
|
446 |
'extract_flat': True,
|
|
|
827 |
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
828 |
|
829 |
if response.status_code == 200:
|
830 |
+
global summary
|
831 |
summary = response.json()['choices'][0]['message']['content'].strip()
|
832 |
logging.debug("openai: Summarization successful")
|
833 |
print("Summarization successful.")
|
|
|
887 |
logging.debug("anthropic: Post submittal successful")
|
888 |
response_data = response.json()
|
889 |
try:
|
890 |
+
global summary
|
891 |
summary = response_data['content'][0]['text'].strip()
|
892 |
logging.debug("anthropic: Summarization successful")
|
893 |
print("Summary processed successfully.")
|
|
|
948 |
|
949 |
if response.status_code == 200:
|
950 |
if 'text' in response_data:
|
951 |
+
global summary
|
952 |
summary = response_data['text'].strip()
|
953 |
+
logging.debug(f"cohere: Summarization successful:\n\n{summary}\n\n")
|
954 |
print("Summary processed successfully.")
|
955 |
return summary
|
956 |
else:
|
|
|
1003 |
|
1004 |
if response.status_code == 200:
|
1005 |
if 'choices' in response_data and len(response_data['choices']) > 0:
|
1006 |
+
global summary
|
1007 |
summary = response_data['choices'][0]['message']['content'].strip()
|
1008 |
logging.debug("groq: Summarization successful")
|
1009 |
print("Summarization successful.")
|
|
|
1056 |
if response.status_code == 200:
|
1057 |
# if 'X' in response_data:
|
1058 |
logging.debug(response_data)
|
1059 |
+
global summary
|
1060 |
summary = response_data['content'].strip()
|
1061 |
logging.debug("llama: Summarization successful")
|
1062 |
print("Summarization successful.")
|
|
|
1106 |
|
1107 |
if response.status_code == 200:
|
1108 |
if 'results' in response_data and len(response_data['results']) > 0:
|
1109 |
+
global summary
|
1110 |
summary = response_data['results'][0]['text'].strip()
|
1111 |
logging.debug("kobold: Summarization successful")
|
1112 |
print("Summarization successful.")
|
|
|
1159 |
|
1160 |
if response.status_code == 200:
|
1161 |
response_data = response.json()
|
1162 |
+
global summary
|
1163 |
summary = response.json()['choices'][0]['message']['content']
|
1164 |
logging.debug("ooba: Summarization successful")
|
1165 |
print("Summarization successful.")
|
|
|
1193 |
# Only to be used when configured with Gradio for HF Space
|
1194 |
def summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt):
|
1195 |
logging.debug(f"huggingface: Summarization process starting...")
|
1196 |
+
client = InferenceClient()
|
1197 |
|
1198 |
+
#model = "microsoft/Phi-3-mini-128k-instruct"
|
1199 |
+
model = "CohereForAI/c4ai-command-r-plus"
|
1200 |
API_URL = f"https://api-inference.huggingface.co/models/{model}"
|
1201 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
1202 |
|
1203 |
+
client = InferenceClient(model=f"{model}", token=f"{huggingface_api_key}")
|
1204 |
+
|
1205 |
+
response = client.post(json={"inputs": "The goal of life is [MASK]."}, model="bert-base-uncased")
|
1206 |
+
|
1207 |
with open(json_file_path, 'r') as file:
|
1208 |
segments = json.load(file)
|
1209 |
text = ''.join([segment['text'] for segment in segments])
|
1210 |
|
1211 |
+
hf_prompt = text + "\n\n\n\n" + custom_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1212 |
|
1213 |
if huggingface_api_key == "":
|
1214 |
api_key = os.getenv(HF_TOKEN)
|
|
|
1225 |
logging.debug("HUGGINGFACE API KEY CHECK #2: " + huggingface_api_key)
|
1226 |
|
1227 |
logging.debug("huggingface: Submitting request...")
|
1228 |
+
response = client.text_generation(prompt=hf_prompt, max_new_tokens=4096)
|
1229 |
+
if response is not None:
|
1230 |
+
return response
|
1231 |
+
#if response == FIXME:
|
1232 |
+
#logging.debug("huggingface: Summarization successful")
|
1233 |
+
#print("Summarization successful.")
|
1234 |
+
#return response
|
1235 |
+
#elif Bad Stuff:
|
1236 |
+
# logging.debug(f"huggingface: Model is currently loading...{response.status_code}: {response.text}")
|
1237 |
+
# global waiting_summary
|
1238 |
+
# pretty_json = json.dumps(json.loads(response.text), indent=4) # Prettify JSON
|
1239 |
+
# waiting_summary = f" {pretty_json} " # Use prettified JSON
|
1240 |
+
# return waiting_summary
|
1241 |
else:
|
1242 |
+
logging.error(f"huggingface: Summarization failed with status code {response}")
|
1243 |
+
return f"Failed to process summary, huggingface library error: {response}"
|
1244 |
except Exception as e:
|
1245 |
logging.error("huggingface: Error in processing: %s", str(e))
|
1246 |
print(f"Error occurred while processing summary with huggingface: {str(e)}")
|
|
|
1274 |
# Hide all inputs below URL
|
1275 |
return [gr.update(visible=False)] * 9
|
1276 |
|
1277 |
+
|
1278 |
# https://www.gradio.app/guides/controlling-layout
|
1279 |
def launch_ui(demo_mode=False):
|
1280 |
whisper_models = ["small.en", "medium.en", "large"]
|
|
|
1308 |
offset_input = gr.Number(value=0, label="Offset (Seconds into the video to start transcribing at)",
|
1309 |
visible=False)
|
1310 |
api_name_input = gr.Dropdown(
|
1311 |
+
choices=[None, "huggingface", "openai", "anthropic", "cohere", "groq", "llama", "kobold", "ooba"],
|
1312 |
+
value=None,
|
1313 |
label="API Name (Mandatory Unless you just want a Transcription)", visible=True)
|
1314 |
api_key_input = gr.Textbox(label="API Key (Mandatory if API Name is specified)",
|
1315 |
placeholder="Enter your API key here", visible=True)
|
|
|
1372 |
|
1373 |
iface.launch(share=False)
|
1374 |
|
1375 |
+
|
1376 |
#
|
1377 |
#
|
1378 |
#####################################################################################################################################
|
|
|
1445 |
logging.info(f"Transcription complete: {audio_file}")
|
1446 |
|
1447 |
# Perform summarization based on the specified API
|
1448 |
+
logging.debug(f"MAIN: Summarization being performed by {api_name} API")
|
1449 |
json_file_path = audio_file.replace('.wav', '.segments.json')
|
1450 |
if api_name == "huggingface":
|
1451 |
huggingface_api_key = os.getenv('HF_TOKEN').replace('"', '')
|
1452 |
if huggingface_api_key is None:
|
1453 |
+
huggingface_api_key = api_key if api_key else config.get('API', 'huggingface_api_key',
|
1454 |
+
fallback=None)
|
1455 |
try:
|
1456 |
logging.debug(f"MAIN: Trying to summarize with huggingface")
|
1457 |
summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt)
|
1458 |
except requests.exceptions.ConnectionError:
|
1459 |
requests.status_code = "Connection: "
|
1460 |
+
elif api_name == "cohere":
|
1461 |
+
cohere_api_key = os.getenv('COHERE_TOKEN').replace('"', '')
|
1462 |
+
if cohere_api_key is None:
|
1463 |
+
cohere_api_key = api_key if api_key else config.get('API', 'cohere_api_key',
|
1464 |
+
fallback=None)
|
1465 |
+
try:
|
1466 |
+
global summary
|
1467 |
+
logging.debug(f"MAIN: Trying to summarize with Cohere on HuggingFace Spaces")
|
1468 |
+
summary = summarize_with_cohere(cohere_api_key, json_file_path, cohere_model, custom_prompt)
|
1469 |
+
transcription_result['summary'] = summary
|
1470 |
+
logging.info(f"Summary generated using {api_name} API")
|
1471 |
+
save_summary_to_file(summary, json_file_path)
|
1472 |
+
except requests.exceptions.ConnectionError:
|
1473 |
+
requests.status_code = "Connection: "
|
1474 |
+
elif api_name and api_key:
|
1475 |
logging.debug(f"MAIN: Summarization being performed by {api_name}")
|
1476 |
json_file_path = audio_file.replace('.wav', '.segments.json')
|
1477 |
if api_name.lower() == 'openai':
|
|
|
1484 |
elif api_name.lower() == "huggingface":
|
1485 |
huggingface_api_key = os.getenv(HF_TOKEN)
|
1486 |
if huggingface_api_key is None:
|
1487 |
+
huggingface_api_key = api_key if api_key else config.get('API', 'huggingface_api_key',
|
1488 |
+
fallback=None)
|
1489 |
try:
|
1490 |
logging.debug(f"MAIN: Trying to summarize with huggingface")
|
1491 |
summarize_with_huggingface(huggingface_api_key, json_file_path, custom_prompt)
|
1492 |
except requests.exceptions.ConnectionError:
|
1493 |
requests.status_code = "Connection: "
|
1494 |
elif api_name.lower() == "anthropic":
|
1495 |
+
anthropic_api_key = api_key if api_key else config.get('API', 'anthropic_api_key',
|
1496 |
+
fallback=None)
|
1497 |
try:
|
1498 |
logging.debug(f"MAIN: Trying to summarize with anthropic")
|
1499 |
+
summary = summarize_with_claude(anthropic_api_key, json_file_path, anthropic_model,
|
1500 |
+
custom_prompt)
|
1501 |
except requests.exceptions.ConnectionError:
|
1502 |
requests.status_code = "Connection: "
|
1503 |
elif api_name.lower() == "cohere":
|
|
|
1542 |
logging.warning(f"Unsupported API: {api_name}")
|
1543 |
summary = None
|
1544 |
|
1545 |
+
print(f"MAIN: #1 - Summary: {summary}")
|
1546 |
if summary:
|
1547 |
transcription_result['summary'] = summary
|
1548 |
logging.info(f"Summary generated using {api_name} API")
|