naimulislam commited on
Commit
6c25357
·
verified ·
1 Parent(s): 9ba3b34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -62
app.py CHANGED
@@ -1,67 +1,40 @@
1
  import streamlit as st
2
  import os
3
  import requests
4
- from fastapi import FastAPI, Request
5
- from fastapi.responses import StreamingResponse
6
- import uvicorn
7
- from threading import Thread
8
 
9
- # --- FASTAPI PROXY LOGIC ---
10
- # This part handles the API traffic without asking for a password
11
- api_app = FastAPI()
12
-
13
- OLLAMA_URL = "http://localhost:11434"
14
-
15
- @api_app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
16
- async def proxy_ollama(path: Request, path_name: str):
17
- url = f"{OLLAMA_URL}/api/{path_name}"
18
- method = path.method
19
- contents = await path.body()
20
- headers = dict(path.headers)
21
-
22
- # Remove the host header to avoid confusion
23
- headers.pop("host", None)
24
-
25
- resp = requests.request(method, url, data=contents, headers=headers, stream=True)
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- return StreamingResponse(
28
- resp.iter_content(chunk_size=1024),
29
- status_code=resp.status_code,
30
- headers=dict(resp.headers)
31
- )
32
-
33
- def run_api():
34
- uvicorn.run(api_app, host="0.0.0.0", port=7860)
35
-
36
- # --- STREAMLIT UI LOGIC ---
37
- # This part handles the visual interface on a hidden port
38
- def run_ui():
39
- st.set_page_config(page_title="Core Interface", page_icon="⚡")
40
-
41
- # Simple CSS for your premium look
42
- st.markdown("<style>.main { background-color: #121212; color: #e0e0e0; }</style>", unsafe_allow_html=True)
43
-
44
- SECRET_PIN = os.getenv("PIN", "1234")
45
-
46
- if "authenticated" not in st.session_state:
47
- st.session_state["authenticated"] = False
48
-
49
- if not st.session_state["authenticated"]:
50
- st.title("System Locked")
51
- user_input = st.text_input("Enter Access Key", type="password")
52
- if st.button("Authenticate"):
53
- if user_input == SECRET_PIN:
54
- st.session_state["authenticated"] = True
55
- st.rerun()
56
- else:
57
- st.error("Access Denied.")
58
- else:
59
- st.title("⚡ Node Status: Online")
60
- st.success("API is public at /api/generate")
61
- st.info("Ollama is running on backend port 11434")
62
-
63
- # This is a bit of a trick: when HF loads port 7860,
64
- # it will see the FastAPI app. We will use a sub-route for the UI.
65
- if __name__ == "__main__":
66
- # If you are running this via streamlit, it executes the UI
67
- run_ui()
 
1
  import streamlit as st
2
  import os
3
  import requests
 
 
 
 
4
 
5
+ st.set_page_config(page_title="Core Interface", page_icon="⚡")
6
+
7
+ # Premium look
8
+ st.markdown("""
9
+ <style>
10
+ .main { background-color: #0e1117; color: #ffffff; }
11
+ .stTextInput > div > div > input { background-color: #262730; color: white; }
12
+ </style>
13
+ """, unsafe_allow_html=True)
14
+
15
+ PIN = os.getenv("PIN", "1234")
16
+
17
+ if "auth" not in st.session_state:
18
+ st.session_state.auth = False
19
+
20
+ if not st.session_state.auth:
21
+ st.title("🔒 System Restricted")
22
+ val = st.text_input("Access Key", type="password")
23
+ if st.button("Unlock"):
24
+ if val == PIN:
25
+ st.session_state.auth = True
26
+ st.rerun()
27
+ else:
28
+ st.error("Incorrect.")
29
+ else:
30
+ st.title("⚡ Server Node Active")
31
+ st.success("API Path: `/api/generate` (Unlocked)")
32
+ st.info("Model: `qwen2.5:0.5b` pre-loaded.")
33
 
34
+ # Status Check
35
+ if st.button("Check Ollama Health"):
36
+ try:
37
+ r = requests.get("http://localhost:11434/api/tags")
38
+ st.json(r.json())
39
+ except:
40
+ st.error("Backend offline.")