diff --git a/main.py b/main.py index 572eb9a..3a95f80 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ import threading # GPT Library import g4f from g4f.api import run_api +from g4f.errors import MissingAuthError, NoValidHarFileError, ProviderNotFoundError, RetryProviderError # Server from flask import Flask, redirect, render_template @@ -450,42 +451,50 @@ async def index() -> str: proxy = f"{proxy['protocol']}://{proxy['username']}:{proxy['password']}@{proxy['ip']}:{proxy['port']}" print("Proxy: " + proxy) - if (args.provider == "Auto"): - response = ( - await g4f.ChatCompletion.create_async( - model=args.model, - messages=message_history, - cookies=cookies, - proxy=proxy + try: + if (args.provider == "Auto"): + response = ( + await g4f.ChatCompletion.create_async( + model=args.model, + messages=message_history, + cookies=cookies, + proxy=proxy + ) ) - ) - else: - response = ( - await g4f.ChatCompletion.create_async( - model=args.model, - provider=args.provider, - messages=message_history, - cookies=cookies, - proxy=proxy + else: + response = ( + await g4f.ChatCompletion.create_async( + model=args.model, + provider=args.provider, + messages=message_history, + cookies=cookies, + proxy=proxy + ) ) - ) - print(response) + print(response) - #Joins the response into a single string - resp_str = "" - for message in response: - resp_str += message + #Joins the response into a single string + resp_str = "" + for message in response: + resp_str += message - # Cleans the response from the resources links - if (args.remove_sources): - if re.search(r"\[\^[0-9]+\^\]\[[0-9]+\]", resp_str): - resp_str = resp_str.split("\n\n") - if len(resp_str) > 1: - resp_str.pop(0) - resp_str = re.sub(r"\[\^[0-9]+\^\]\[[0-9]+\]", "", str(resp_str[0])) - # Returns the response - return resp_str + # Cleans the response from the resources links + if (args.remove_sources): + if re.search(r"\[\^[0-9]+\^\]\[[0-9]+\]", resp_str): + resp_str = resp_str.split("\n\n") + if len(resp_str) > 1: + resp_str.pop(0) + resp_str = re.sub(r"\[\^[0-9]+\^\]\[[0-9]+\]", "", str(resp_str[0])) + # Returns the response + return resp_str + except (MissingAuthError, NoValidHarFileError, ProviderNotFoundError, RetryProviderError) as e: + print(f"[X] Provider error: {type(e).__name__}: {e}") + print("[!] Returning empty JSON response") + return '{"translation_text": "", "short_text": "", "title": "", "category": ""}' + except Exception as e: + print(f"[X] Unexpected error: {type(e).__name__}: {e}") + return '{"translation_text": "", "short_text": "", "title": "", "category": ""}' # return "
" + resp + "
" # Uncomment if preferred def auth(): diff --git a/requirements.txt b/requirements.txt index eaac9b9..6ab6ab5 100644 Binary files a/requirements.txt and b/requirements.txt differ