ghn22 commited on
Commit
ce72fd9
ยท
verified ยท
1 Parent(s): ba8e529

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +13 -0
  2. app.py +115 -0
  3. gitattributes +35 -0
  4. requirements.txt +3 -0
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gemma 4 Chat
3
+ emoji: ๐Ÿš€
4
+ colorFrom: blue
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 6.19.0
8
+ python_version: "3.10"
9
+ app_file: app.py
10
+ pinned: false
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from llama_cpp import Llama
5
+
6
+ # 1. Fetch your secret credentials from the environment
7
+ ADMIN_USER = os.environ.get("ADMIN_USER")
8
+ ADMIN_PASS = os.environ.get("ADMIN_PASS")
9
+
10
+ # 2. Define the path inside your persistent storage for Phi-4 Mini
11
+ model_path = "/data/Phi-4-mini-instruct-Q4_K_M.gguf"
12
+
13
+ # Auto-download from the UNGATED Unsloth public repository if missing
14
+ if not os.path.exists(model_path):
15
+ print("\n๐Ÿ“ฆ Initializing Gated-Bypass CPU Model Setup...")
16
+ hf_hub_download(
17
+ repo_id="unsloth/Phi-4-mini-instruct-GGUF",
18
+ filename="Phi-4-mini-instruct-Q4_K_M.gguf",
19
+ local_dir="/data"
20
+ )
21
+ print("โœ… Download complete!\n")
22
+
23
+ print("Loading model from persistent storage...")
24
+ llm = Llama(
25
+ model_path=model_path,
26
+ n_ctx=2048,
27
+ n_threads=2,
28
+ n_batch=512,
29
+ verbose=False
30
+ )
31
+
32
+ # 3. Define the Chat Engine Logic
33
+ def respond(message, history):
34
+ messages = [
35
+ {"role": "system", "content": "You are a helpful, intelligent AI assistant. Keep responses concise."}
36
+ ]
37
+
38
+ # ROBUST GRADIO 6.0 HISTORY PARSING
39
+ # Iterates over individual message objects rather than historic pairs
40
+ for msg in history:
41
+ if hasattr(msg, "role") and hasattr(msg, "content"):
42
+ role = msg.role
43
+ content = msg.content
44
+ elif isinstance(msg, dict):
45
+ role = msg.get("role")
46
+ content = msg.get("content")
47
+ else:
48
+ continue
49
+
50
+ if role and content:
51
+ messages.append({"role": str(role).lower(), "content": str(content)})
52
+
53
+ # Safe current user text extraction (handles strings, objects, or dict messages)
54
+ user_text = message
55
+ if hasattr(message, "content"):
56
+ user_text = message.content
57
+ elif isinstance(message, dict) and "content" in message:
58
+ user_text = message["content"]
59
+
60
+ messages.append({"role": "user", "content": str(user_text)})
61
+
62
+ response = llm.create_chat_completion(
63
+ messages=messages,
64
+ max_tokens=512,
65
+ temperature=0.7,
66
+ stream=True
67
+ )
68
+
69
+ partial_message = ""
70
+ for chunk in response:
71
+ if "choices" in chunk and len(chunk["choices"]) > 0:
72
+ delta = chunk["choices"][0].get("delta", {})
73
+ if "content" in delta:
74
+ partial_message += delta["content"]
75
+ yield partial_message
76
+
77
+ # 4. Build the Application with custom Login Gate Routing
78
+ with gr.Blocks() as demo:
79
+
80
+ has_auth = bool(ADMIN_USER and ADMIN_PASS)
81
+
82
+ # CONTAINER A: The Secure Login Box UI
83
+ with gr.Column(visible=has_auth) as login_container:
84
+ gr.Markdown("# ๐Ÿ”’ Protected Workspace\nPlease enter your administrator credentials to access the CPU environment.")
85
+ username_input = gr.Textbox(label="Username", placeholder="Enter username...")
86
+ password_input = gr.Textbox(label="Password", type="password", placeholder="Enter password...")
87
+ login_button = gr.Button("Access System", variant="primary")
88
+ error_output = gr.Markdown(visible=False)
89
+
90
+ # CONTAINER B: The Main Chat Application UI
91
+ with gr.Column(visible=not has_auth) as app_container:
92
+ gr.ChatInterface(
93
+ fn=respond,
94
+ title="Phi-4 Mini 3.8B Chat (CPU Optimized)",
95
+ description="Running instantly via persistent local storage with custom 2 vCPU optimizations."
96
+ )
97
+
98
+ # Core Verification Function
99
+ def handle_login(username, password):
100
+ if username == ADMIN_USER and password == ADMIN_PASS:
101
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
102
+ else:
103
+ return gr.update(visible=True), gr.update(visible=False), gr.update(value="โŒ **Access Denied:** Invalid username or password.", visible=True)
104
+
105
+ # Link button click to verification function
106
+ login_button.click(
107
+ fn=handle_login,
108
+ inputs=[username_input, password_input],
109
+ outputs=[login_container, app_container, error_output]
110
+ )
111
+
112
+ # 5. Launch the Space safely without launcher auth conflicts
113
+ if __name__ == "__main__":
114
+ # Theme configuration parameter handled at launch level to comply with Gradio 6 updates
115
+ demo.launch(server_name="0.0.0.0", server_port=7860, theme="soft")
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ huggingface_hub
3
+ https://huggingface.co/Luigi/llama-cpp-python-wheels-hf-spaces-free-cpu/resolve/main/llama_cpp_python-0.3.22-cp310-cp310-linux_x86_64.whl