Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -6,6 +6,7 @@ from huggingface_hub import InferenceClient | |
| 6 | 
             
            # Helper to read a secret (fallback is useful when you run locally)
         | 
| 7 | 
             
            # ----------------------------------------------------------------------
         | 
| 8 | 
             
            def _secret(key: str, fallback: str = "") -> str:
         | 
|  | |
| 9 | 
             
                return os.getenv(key, fallback)
         | 
| 10 |  | 
| 11 |  | 
| @@ -18,26 +19,32 @@ def respond( | |
| 18 | 
             
                max_tokens: int,
         | 
| 19 | 
             
                temperature: float,
         | 
| 20 | 
             
                top_p: float,
         | 
| 21 | 
            -
                hf_token: gr.OAuthToken,
         | 
| 22 | 
             
            ):
         | 
| 23 | 
             
                """
         | 
| 24 | 
             
                Generate a response using the HuggingFace Inference API.
         | 
| 25 |  | 
| 26 | 
            -
                 | 
| 27 | 
            -
                 | 
| 28 | 
             
                """
         | 
| 29 | 
             
                # 1️⃣  Load the system prompt (fallback = generic assistant)
         | 
| 30 | 
             
                system_message = _secret("prec_chat", "You are a helpful assistant.")
         | 
| 31 |  | 
| 32 | 
            -
                # 2️⃣   | 
| 33 | 
            -
                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 34 |  | 
| 35 | 
            -
                #  | 
| 36 | 
             
                messages = [{"role": "system", "content": system_message}]
         | 
| 37 | 
            -
                messages.extend(history)                     # previous turns
         | 
| 38 | 
            -
                messages.append({"role": "user", "content": message})  # current query
         | 
| 39 |  | 
| 40 | 
            -
                #  | 
| 41 | 
             
                response = ""
         | 
| 42 | 
             
                for chunk in client.chat_completion(
         | 
| 43 | 
             
                    messages,
         | 
| @@ -85,7 +92,9 @@ with gr.Blocks() as demo: | |
| 85 | 
             
            # Launch – protect the UI with the credentials from secrets.
         | 
| 86 | 
             
            # ----------------------------------------------------------------------
         | 
| 87 | 
             
            if __name__ == "__main__":
         | 
| 88 | 
            -
                #  | 
|  | |
|  | |
| 89 | 
             
                allowed_user = _secret("CHAT_USER")
         | 
| 90 | 
             
                allowed_pass = _secret("CHAT_PASS")
         | 
| 91 |  | 
| @@ -95,12 +104,15 @@ if __name__ == "__main__": | |
| 95 | 
             
                        "Add CHAT_USER and CHAT_PASS to secrets.toml (or via the HF Spaces UI)."
         | 
| 96 | 
             
                    )
         | 
| 97 |  | 
|  | |
|  | |
|  | |
| 98 | 
             
                demo.launch(
         | 
| 99 | 
             
                    auth=(allowed_user, allowed_pass),   # <-- Gradio's built‑in basic auth
         | 
| 100 | 
            -
                    #  | 
| 101 | 
            -
                     | 
| 102 | 
            -
                    #  | 
| 103 | 
            -
                     | 
| 104 | 
            -
                    # Optional  | 
| 105 | 
            -
                     | 
| 106 | 
             
                )
         | 
|  | |
| 6 | 
             
            # Helper to read a secret (fallback is useful when you run locally)
         | 
| 7 | 
             
            # ----------------------------------------------------------------------
         | 
| 8 | 
             
            def _secret(key: str, fallback: str = "") -> str:
         | 
| 9 | 
            +
                """Return the value of a secret or the supplied fallback."""
         | 
| 10 | 
             
                return os.getenv(key, fallback)
         | 
| 11 |  | 
| 12 |  | 
|  | |
| 19 | 
             
                max_tokens: int,
         | 
| 20 | 
             
                temperature: float,
         | 
| 21 | 
             
                top_p: float,
         | 
|  | |
| 22 | 
             
            ):
         | 
| 23 | 
             
                """
         | 
| 24 | 
             
                Generate a response using the HuggingFace Inference API.
         | 
| 25 |  | 
| 26 | 
            +
                * System prompt = secret `prec_chat`
         | 
| 27 | 
            +
                * HF inference token = secret `HF_TOKEN`
         | 
| 28 | 
             
                """
         | 
| 29 | 
             
                # 1️⃣  Load the system prompt (fallback = generic assistant)
         | 
| 30 | 
             
                system_message = _secret("prec_chat", "You are a helpful assistant.")
         | 
| 31 |  | 
| 32 | 
            +
                # 2️⃣  Load the HF inference token
         | 
| 33 | 
            +
                hf_token = _secret("HF_TOKEN")
         | 
| 34 | 
            +
                if not hf_token:
         | 
| 35 | 
            +
                    raise RuntimeError(
         | 
| 36 | 
            +
                        "HF_TOKEN not found in secrets. Add it to secrets.toml (or via the Space UI)."
         | 
| 37 | 
            +
                    )
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                # 3️⃣  Initialise the HF inference client
         | 
| 40 | 
            +
                client = InferenceClient(token=hf_token, model="openai/gpt-oss-20b")
         | 
| 41 |  | 
| 42 | 
            +
                # 4️⃣  Build the message list for the chat‑completion endpoint
         | 
| 43 | 
             
                messages = [{"role": "system", "content": system_message}]
         | 
| 44 | 
            +
                messages.extend(history)                     # previous conversation turns
         | 
| 45 | 
            +
                messages.append({"role": "user", "content": message})  # current user query
         | 
| 46 |  | 
| 47 | 
            +
                # 5️⃣  Stream the response back to the UI
         | 
| 48 | 
             
                response = ""
         | 
| 49 | 
             
                for chunk in client.chat_completion(
         | 
| 50 | 
             
                    messages,
         | 
|  | |
| 92 | 
             
            # Launch – protect the UI with the credentials from secrets.
         | 
| 93 | 
             
            # ----------------------------------------------------------------------
         | 
| 94 | 
             
            if __name__ == "__main__":
         | 
| 95 | 
            +
                # ------------------------------------------------------------------
         | 
| 96 | 
            +
                # 1️⃣  Pull the allowed credentials from secrets (fail fast if missing)
         | 
| 97 | 
            +
                # ------------------------------------------------------------------
         | 
| 98 | 
             
                allowed_user = _secret("CHAT_USER")
         | 
| 99 | 
             
                allowed_pass = _secret("CHAT_PASS")
         | 
| 100 |  | 
|  | |
| 104 | 
             
                        "Add CHAT_USER and CHAT_PASS to secrets.toml (or via the HF Spaces UI)."
         | 
| 105 | 
             
                    )
         | 
| 106 |  | 
| 107 | 
            +
                # ------------------------------------------------------------------
         | 
| 108 | 
            +
                # 2️⃣  Launch
         | 
| 109 | 
            +
                # ------------------------------------------------------------------
         | 
| 110 | 
             
                demo.launch(
         | 
| 111 | 
             
                    auth=(allowed_user, allowed_pass),   # <-- Gradio's built‑in basic auth
         | 
| 112 | 
            +
                    ssr_mode=False,                     # <-- avoids the i18n locale error
         | 
| 113 | 
            +
                    # In a Space we **must not** set share=True (Spaces already give a public URL)
         | 
| 114 | 
            +
                    # If you run locally and want a shareable link, add share=True here.
         | 
| 115 | 
            +
                    server_name="0.0.0.0",              # listen on all interfaces (needed in containers)
         | 
| 116 | 
            +
                    # Optional: give the app a nice title
         | 
| 117 | 
            +
                    # title="Secure Chatbot",
         | 
| 118 | 
             
                )
         | 
