FadeClip commited on
Commit
8c8ff71
·
1 Parent(s): 2c97677

Remove API key functionality from application

Browse files
Files changed (4) hide show
  1. chat.html +0 -7
  2. main.py +2 -44
  3. static/script.js +1 -49
  4. static/style.css +0 -39
chat.html CHANGED
@@ -10,13 +10,6 @@
10
  <div id="chat-container">
11
  <div id="header">
12
  <h1>Ollama Chat</h1>
13
- <div id="api-key-section">
14
- <label for="api-key-input">API Key:</label>
15
- <input type="password" id="api-key-input" placeholder="Enter API key">
16
- <button id="save-api-key">Save</button>
17
- <button id="regenerate-key">Regenerate</button>
18
- <div id="api-key-info">Check server console for auto-generated API key</div>
19
- </div>
20
  </div>
21
  <div id="chat-window">
22
  <ul id="message-list"></ul>
 
10
  <div id="chat-container">
11
  <div id="header">
12
  <h1>Ollama Chat</h1>
 
 
 
 
 
 
 
13
  </div>
14
  <div id="chat-window">
15
  <ul id="message-list"></ul>
main.py CHANGED
@@ -1,33 +1,17 @@
1
- import secrets
2
  import os
3
  import requests
4
- from fastapi import FastAPI, Request, HTTPException, Depends
5
  from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse
6
  from fastapi.staticfiles import StaticFiles
7
- from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
8
-
9
- # Generate a secure API key if not provided as environment variable
10
- API_KEY = os.getenv("OLLAMA_API_KEY") or secrets.token_urlsafe(32)
11
- print(f"API Key: {API_KEY}") # This will be shown in the console during startup
12
 
13
  OLLAMA_API_URL = "http://localhost:11434"
14
 
15
  app = FastAPI()
16
- security = HTTPBearer()
17
 
18
  app.mount("/static", StaticFiles(directory="static"), name="static")
19
 
20
- def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
21
- """Verify that the provided API key is valid"""
22
- if credentials.credentials != current_api_key:
23
- raise HTTPException(
24
- status_code=401,
25
- detail="Invalid API key"
26
- )
27
- return credentials.credentials
28
-
29
  @app.post("/chat_api")
30
- async def chat_endpoint(request: Request, api_key: str = Depends(verify_api_key)):
31
  body = await request.json()
32
  model = body.get("model", "qwen3:1.7b")
33
  prompt = body.get("prompt")
@@ -58,29 +42,3 @@ async def root():
58
  @app.get("/chat")
59
  async def chat_page():
60
  return FileResponse('chat.html')
61
-
62
- @app.get("/api/health")
63
- async def health_check():
64
- """Health check endpoint to verify API is running"""
65
- return {"status": "ok"}
66
-
67
- # Store the current API key in memory
68
- current_api_key = API_KEY
69
-
70
- @app.get("/api/config")
71
- async def get_config():
72
- """Return configuration information including the API key status (without revealing the key)"""
73
- has_env_key = bool(os.getenv("OLLAMA_API_KEY"))
74
- return {
75
- "api_key_set": has_env_key,
76
- "model_default": "qwen3:1.7b"
77
- }
78
-
79
- @app.post("/api/regenerate_key")
80
- async def regenerate_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
81
- """Regenerate the API key (requires valid current API key)"""
82
- global current_api_key
83
- new_key = secrets.token_urlsafe(32)
84
- current_api_key = new_key
85
- print(f"New API Key: {new_key}") # Display the new key in the console
86
- return {"message": "API key regenerated successfully", "key": new_key}
 
 
1
  import os
2
  import requests
3
+ from fastapi import FastAPI, Request, HTTPException
4
  from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse
5
  from fastapi.staticfiles import StaticFiles
 
 
 
 
 
6
 
7
  OLLAMA_API_URL = "http://localhost:11434"
8
 
9
  app = FastAPI()
 
10
 
11
  app.mount("/static", StaticFiles(directory="static"), name="static")
12
 
 
 
 
 
 
 
 
 
 
13
  @app.post("/chat_api")
14
+ async def chat_endpoint(request: Request):
15
  body = await request.json()
16
  model = body.get("model", "qwen3:1.7b")
17
  prompt = body.get("prompt")
 
42
  @app.get("/chat")
43
  async def chat_page():
44
  return FileResponse('chat.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/script.js CHANGED
@@ -1,53 +1,6 @@
1
  const messageList = document.getElementById('message-list');
2
  const messageInput = document.getElementById('message-input');
3
  const sendButton = document.getElementById('send-button');
4
- const apiKeyInput = document.getElementById('api-key-input');
5
- const saveApiKeyButton = document.getElementById('save-api-key');
6
- const regenerateKeyButton = document.getElementById('regenerate-key');
7
-
8
- // Get API key from localStorage
9
- let apiKey = localStorage.getItem('ollama_api_key');
10
- if (apiKey) {
11
- apiKeyInput.value = apiKey;
12
- }
13
-
14
- // Save API key button event
15
- saveApiKeyButton.addEventListener('click', () => {
16
- const newApiKey = apiKeyInput.value.trim();
17
- if (newApiKey) {
18
- localStorage.setItem('ollama_api_key', newApiKey);
19
- apiKey = newApiKey;
20
- alert('API key saved successfully!');
21
- } else {
22
- alert('Please enter a valid API key');
23
- }
24
- });
25
-
26
- // Regenerate API key button event
27
- regenerateKeyButton.addEventListener('click', () => {
28
- if (confirm('Are you sure you want to regenerate the API key? This will invalidate the current key.')) {
29
- fetch('/api/regenerate_key', {
30
- method: 'POST',
31
- headers: {
32
- 'Content-Type': 'application/json',
33
- 'Authorization': `Bearer ${apiKey}`
34
- }
35
- })
36
- .then(response => response.json())
37
- .then(data => {
38
- if (data.key) {
39
- alert(`New API key generated. Please update your API key in the input field. The new key is (also shown in server console): ${data.key}`);
40
- // Do not automatically update the API key in localStorage for security reasons
41
- } else {
42
- alert('Failed to regenerate API key. Please check the server.');
43
- }
44
- })
45
- .catch(error => {
46
- console.error('Error regenerating API key:', error);
47
- alert('Error regenerating API key. Please check the server.');
48
- });
49
- }
50
- });
51
 
52
  // Send message function
53
  sendButton.addEventListener('click', sendMessage);
@@ -71,8 +24,7 @@ function sendMessage() {
71
  fetch('/chat_api', {
72
  method: 'POST',
73
  headers: {
74
- 'Content-Type': 'application/json',
75
- 'Authorization': `Bearer ${apiKey}`
76
  },
77
  body: JSON.stringify({
78
  model: 'qwen3:1.7b', // You can change the model here
 
1
  const messageList = document.getElementById('message-list');
2
  const messageInput = document.getElementById('message-input');
3
  const sendButton = document.getElementById('send-button');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  // Send message function
6
  sendButton.addEventListener('click', sendMessage);
 
24
  fetch('/chat_api', {
25
  method: 'POST',
26
  headers: {
27
+ 'Content-Type': 'application/json'
 
28
  },
29
  body: JSON.stringify({
30
  model: 'qwen3:1.7b', // You can change the model here
static/style.css CHANGED
@@ -29,45 +29,6 @@ body {
29
  font-size: 1.5em;
30
  }
31
 
32
- #api-key-section {
33
- display: flex;
34
- align-items: center;
35
- gap: 10px;
36
- flex-wrap: wrap;
37
- }
38
-
39
- #api-key-input {
40
- padding: 5px;
41
- border: 1px solid #ccc;
42
- border-radius: 3px;
43
- }
44
-
45
- #api-key-info {
46
- font-size: 0.8em;
47
- color: #666;
48
- background-color: #f0f0f0;
49
- padding: 3px 6px;
50
- border-radius: 3px;
51
- }
52
-
53
- #save-api-key {
54
- background-color: #007cba;
55
- color: white;
56
- border: none;
57
- padding: 5px 10px;
58
- border-radius: 3px;
59
- cursor: pointer;
60
- }
61
-
62
- #regenerate-key {
63
- background-color: #ff9800;
64
- color: white;
65
- border: none;
66
- padding: 5px 10px;
67
- border-radius: 3px;
68
- cursor: pointer;
69
- }
70
-
71
  #chat-window {
72
  flex-grow: 1;
73
  overflow-y: auto;
 
29
  font-size: 1.5em;
30
  }
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  #chat-window {
33
  flex-grow: 1;
34
  overflow-y: auto;