Spaces:
Runtime error
Runtime error
Simplify demo creation - remove try/except wrapper for cleaner module-level assignment
Browse files
app.py
CHANGED
|
@@ -833,54 +833,35 @@ def main():
|
|
| 833 |
# For Hugging Face Spaces: create demo at module level
|
| 834 |
# Spaces will import this module and look for a 'demo' variable
|
| 835 |
def create_demo_for_spaces():
|
| 836 |
-
"""Create demo for Hugging Face Spaces"""
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
bot = RAGBot(args)
|
| 866 |
-
|
| 867 |
-
# Don't load the model - we'll use Inference API
|
| 868 |
-
# Just verify vector DB is available
|
| 869 |
-
if bot.vector_retriever is None:
|
| 870 |
-
raise Exception("Vector database not available")
|
| 871 |
-
|
| 872 |
-
# Use Inference API instead of loading model
|
| 873 |
-
interface_demo = create_interface(bot, use_inference_api=True)
|
| 874 |
-
logger.info("Demo interface created successfully")
|
| 875 |
-
return interface_demo
|
| 876 |
-
except Exception as e:
|
| 877 |
-
logger.error(f"Error creating demo for Spaces: {e}", exc_info=True)
|
| 878 |
-
import traceback
|
| 879 |
-
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 880 |
-
# Return a simple error demo
|
| 881 |
-
with gr.Blocks() as error_demo:
|
| 882 |
-
gr.Markdown(f"# Error Initializing Chatbot\n\nAn error occurred: {str(e)}\n\nPlease check the logs for details.")
|
| 883 |
-
return error_demo
|
| 884 |
|
| 885 |
# For Hugging Face Spaces: create demo at module level
|
| 886 |
# Spaces will import this module and look for a 'demo' variable
|
|
@@ -891,25 +872,13 @@ IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
|
|
| 891 |
|
| 892 |
# For Hugging Face Spaces: create demo at module level
|
| 893 |
# Spaces will import this module and look for a 'demo' variable
|
| 894 |
-
#
|
|
|
|
|
|
|
| 895 |
if IS_SPACES:
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
logger.info(f"Demo created successfully. Type: {type(demo)}")
|
| 900 |
-
if demo is None:
|
| 901 |
-
raise Exception("Demo creation returned None")
|
| 902 |
-
if not isinstance(demo, gr.Blocks):
|
| 903 |
-
raise Exception(f"Demo is not a Gradio Blocks object, got {type(demo)}")
|
| 904 |
-
logger.info("✅ Demo is ready for Spaces")
|
| 905 |
-
except Exception as e:
|
| 906 |
-
logger.error(f"Error creating demo: {e}", exc_info=True)
|
| 907 |
-
import traceback
|
| 908 |
-
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 909 |
-
# Create error demo
|
| 910 |
-
with gr.Blocks() as error_demo:
|
| 911 |
-
gr.Markdown(f"# Error Initializing Chatbot\n\nAn error occurred: {str(e)}\n\nPlease check the logs.")
|
| 912 |
-
demo = error_demo
|
| 913 |
else:
|
| 914 |
# Local execution: demo will be created in main()
|
| 915 |
demo = None
|
|
|
|
| 833 |
# For Hugging Face Spaces: create demo at module level
|
| 834 |
# Spaces will import this module and look for a 'demo' variable
|
| 835 |
def create_demo_for_spaces():
|
| 836 |
+
"""Create demo for Hugging Face Spaces - called at module level"""
|
| 837 |
+
# Initialize with default args for Spaces
|
| 838 |
+
parser = argparse.ArgumentParser()
|
| 839 |
+
parser.add_argument('--model', type=str, default='meta-llama/Llama-3.2-3B-Instruct')
|
| 840 |
+
parser.add_argument('--vector-db-dir', default='./chroma_db')
|
| 841 |
+
parser.add_argument('--data-dir', default='./Data Resources')
|
| 842 |
+
parser.add_argument('--max-new-tokens', type=int, default=1024)
|
| 843 |
+
parser.add_argument('--temperature', type=float, default=0.2)
|
| 844 |
+
parser.add_argument('--top-p', type=float, default=0.9)
|
| 845 |
+
parser.add_argument('--repetition-penalty', type=float, default=1.1)
|
| 846 |
+
parser.add_argument('--k', type=int, default=5)
|
| 847 |
+
parser.add_argument('--skip-indexing', action='store_true', default=True)
|
| 848 |
+
parser.add_argument('--verbose', action='store_true', default=False)
|
| 849 |
+
parser.add_argument('--share', action='store_true', default=False)
|
| 850 |
+
parser.add_argument('--server-name', type=str, default='0.0.0.0')
|
| 851 |
+
parser.add_argument('--server-port', type=int, default=7860)
|
| 852 |
+
parser.add_argument('--seed', type=int, default=42)
|
| 853 |
+
|
| 854 |
+
args = parser.parse_args([]) # Empty args for Spaces
|
| 855 |
+
args.skip_model_loading = True # Skip model loading, use Inference API
|
| 856 |
+
|
| 857 |
+
# Create bot (will skip model loading)
|
| 858 |
+
bot = RAGBot(args)
|
| 859 |
+
|
| 860 |
+
if bot.vector_retriever is None:
|
| 861 |
+
raise Exception("Vector database not available")
|
| 862 |
+
|
| 863 |
+
# Create and return the demo interface
|
| 864 |
+
return create_interface(bot, use_inference_api=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
# For Hugging Face Spaces: create demo at module level
|
| 867 |
# Spaces will import this module and look for a 'demo' variable
|
|
|
|
| 872 |
|
| 873 |
# For Hugging Face Spaces: create demo at module level
|
| 874 |
# Spaces will import this module and look for a 'demo' variable
|
| 875 |
+
# Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
|
| 876 |
+
# DO NOT call demo.launch() - Spaces handles that automatically
|
| 877 |
+
|
| 878 |
if IS_SPACES:
|
| 879 |
+
logger.info("Initializing for Hugging Face Spaces...")
|
| 880 |
+
demo = create_demo_for_spaces()
|
| 881 |
+
logger.info(f"Demo created: {type(demo)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
else:
|
| 883 |
# Local execution: demo will be created in main()
|
| 884 |
demo = None
|