Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| def get_space_info(): | |
| """ | |
| Retrieves the Hugging Face Space owner and repository name from environment variables. | |
| Returns a formatted string with the information. | |
| """ | |
| space_author = os.getenv("SPACE_AUTHOR_NAME") | |
| space_repo = os.getenv("SPACE_REPO_NAME") | |
| if space_author and space_repo: | |
| return f"This Space is owned by: **{space_author}**\n\n" \ | |
| f"The repository name is: **{space_repo}**\n\n" \ | |
| f"Full Space ID: `{space_author}/{space_repo}`" | |
| else: | |
| return "Not currently running in a Hugging Face Space environment, or required environment variables are not set.\n\n" \ | |
| "To see this work, deploy this code to a Hugging Face Space!" | |
| # Create the Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # Hugging Face Space Info Viewer | |
| Click the button below to retrieve and display the owner and repository name | |
| of this Hugging Face Space. | |
| """ | |
| ) | |
| # Output component to display the information | |
| output_text = gr.Textbox(label="Space Details", lines=5, interactive=False) | |
| # Button to trigger the function | |
| get_info_btn = gr.Button("Get Space Info") | |
| # Define the interaction: when the button is clicked, call get_space_info and update output_text | |
| get_info_btn.click( | |
| fn=get_space_info, | |
| inputs=[], # No inputs needed for this function | |
| outputs=[output_text] | |
| ) | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| demo.launch() |