Spaces:
Runtime error
Runtime error
Create demo unconditionally at module level - ensure Spaces can always detect it
Browse files
app.py
CHANGED
|
@@ -836,51 +836,56 @@ def main():
|
|
| 836 |
# Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
|
| 837 |
# DO NOT call demo.launch() - Spaces handles that automatically
|
| 838 |
|
| 839 |
-
# Check if we're on Spaces
|
| 840 |
-
IS_SPACES =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
|
| 842 |
-
# Create demo at module level
|
| 843 |
-
#
|
| 844 |
-
|
| 845 |
-
|
|
|
|
| 846 |
logger.info("Initializing for Hugging Face Spaces...")
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
|
| 885 |
# For local execution only (not on Spaces)
|
| 886 |
if __name__ == "__main__":
|
|
|
|
| 836 |
# Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
|
| 837 |
# DO NOT call demo.launch() - Spaces handles that automatically
|
| 838 |
|
| 839 |
+
# Check if we're on Spaces (be more permissive - check multiple env vars)
|
| 840 |
+
IS_SPACES = (
|
| 841 |
+
os.getenv("SPACE_ID") is not None or
|
| 842 |
+
os.getenv("SYSTEM") == "spaces" or
|
| 843 |
+
os.getenv("HF_SPACE_ID") is not None
|
| 844 |
+
)
|
| 845 |
|
| 846 |
+
# Create demo unconditionally at module level (like HF docs example)
|
| 847 |
+
# This ensures Spaces can always find it when importing the module
|
| 848 |
+
# For local execution, we'll still create it here but main() will also create its own
|
| 849 |
+
try:
|
| 850 |
+
if IS_SPACES:
|
| 851 |
logger.info("Initializing for Hugging Face Spaces...")
|
| 852 |
+
else:
|
| 853 |
+
logger.info("Initializing for local execution...")
|
| 854 |
+
|
| 855 |
+
# Initialize with default args
|
| 856 |
+
parser = argparse.ArgumentParser()
|
| 857 |
+
parser.add_argument('--model', type=str, default='meta-llama/Llama-3.2-3B-Instruct')
|
| 858 |
+
parser.add_argument('--vector-db-dir', default='./chroma_db')
|
| 859 |
+
parser.add_argument('--data-dir', default='./Data Resources')
|
| 860 |
+
parser.add_argument('--max-new-tokens', type=int, default=1024)
|
| 861 |
+
parser.add_argument('--temperature', type=float, default=0.2)
|
| 862 |
+
parser.add_argument('--top-p', type=float, default=0.9)
|
| 863 |
+
parser.add_argument('--repetition-penalty', type=float, default=1.1)
|
| 864 |
+
parser.add_argument('--k', type=int, default=5)
|
| 865 |
+
parser.add_argument('--skip-indexing', action='store_true', default=True)
|
| 866 |
+
parser.add_argument('--verbose', action='store_true', default=False)
|
| 867 |
+
parser.add_argument('--share', action='store_true', default=False)
|
| 868 |
+
parser.add_argument('--server-name', type=str, default='0.0.0.0')
|
| 869 |
+
parser.add_argument('--server-port', type=int, default=7860)
|
| 870 |
+
parser.add_argument('--seed', type=int, default=42)
|
| 871 |
+
|
| 872 |
+
args = parser.parse_args([]) # Empty args
|
| 873 |
+
args.skip_model_loading = IS_SPACES # Skip model loading on Spaces, use Inference API
|
| 874 |
+
|
| 875 |
+
# Create bot
|
| 876 |
+
bot = RAGBot(args)
|
| 877 |
+
|
| 878 |
+
if bot.vector_retriever is None:
|
| 879 |
+
raise Exception("Vector database not available")
|
| 880 |
+
|
| 881 |
+
# Create the demo interface directly at module level (like HF docs example)
|
| 882 |
+
demo = create_interface(bot, use_inference_api=IS_SPACES)
|
| 883 |
+
logger.info(f"Demo created successfully: {type(demo)}")
|
| 884 |
+
except Exception as e:
|
| 885 |
+
logger.error(f"Error creating demo: {e}", exc_info=True)
|
| 886 |
+
# Create a fallback error demo so Spaces doesn't show blank
|
| 887 |
+
with gr.Blocks() as demo:
|
| 888 |
+
gr.Markdown(f"# Error Initializing Chatbot\n\nAn error occurred while initializing the chatbot.\n\nError: {str(e)}\n\nPlease check the logs for details.")
|
| 889 |
|
| 890 |
# For local execution only (not on Spaces)
|
| 891 |
if __name__ == "__main__":
|