owlninjam commited on
Commit
5d360a3
·
verified ·
1 Parent(s): 657d146

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -375
app.py DELETED
@@ -1,375 +0,0 @@
1
- import streamlit as st
2
- import subprocess
3
- import threading
4
- import time
5
- import requests
6
- import json
7
- from typing import List, Dict
8
- import sys
9
- import os
10
-
11
- # Configuration
12
- API_KEYS = ["sk-adminkey02", "sk-testkey123", "sk-userkey456", "sk-demokey789"]
13
- DEFAULT_API_KEY = "sk-adminkey02"
14
-
15
- def start_fastapi():
16
- """Start FastAPI server in a separate process"""
17
- try:
18
- # Start FastAPI on port 8000 (internal)
19
- subprocess.Popen([
20
- sys.executable, "-m", "uvicorn", "api:app",
21
- "--host", "0.0.0.0",
22
- "--port", "8000",
23
- "--workers", "1"
24
- ])
25
- print("🚀 FastAPI server starting on port 8000...")
26
- except Exception as e:
27
- st.error(f"Failed to start API server: {e}")
28
-
29
- # Start API server when app starts
30
- if 'api_started' not in st.session_state:
31
- st.session_state.api_started = True
32
- threading.Thread(target=start_fastapi, daemon=True).start()
33
- time.sleep(8) # Give API time to start
34
-
35
- def call_api(messages: List[Dict], max_tokens: int = 512, temperature: float = 0.7, api_key: str = DEFAULT_API_KEY):
36
- """Call the internal API"""
37
- try:
38
- response = requests.post(
39
- "http://localhost:8000/v1/chat/completions",
40
- headers={"Authorization": f"Bearer {api_key}"},
41
- json={
42
- "model": "capybarahermes-2.5-mistral-7b",
43
- "messages": messages,
44
- "max_tokens": max_tokens,
45
- "temperature": temperature
46
- },
47
- timeout=120
48
- )
49
-
50
- if response.status_code == 200:
51
- return response.json()
52
- elif response.status_code == 401:
53
- st.error("❌ Invalid API key")
54
- return None
55
- else:
56
- st.error(f"API Error: {response.status_code} - {response.text}")
57
- return None
58
-
59
- except requests.exceptions.RequestException as e:
60
- st.error(f"Failed to connect to API: {e}")
61
- return None
62
-
63
- def check_api_health():
64
- """Check if API is healthy"""
65
- try:
66
- response = requests.get("http://localhost:8000/v1/health", timeout=10)
67
- return response.status_code == 200
68
- except:
69
- return False
70
-
71
- def get_api_info():
72
- """Get API information"""
73
- try:
74
- response = requests.get("http://localhost:8000/v1", timeout=5)
75
- if response.status_code == 200:
76
- return response.json()
77
- except:
78
- pass
79
- return None
80
-
81
- def main():
82
- st.set_page_config(
83
- page_title="🦙 CapybaraHermes Chat",
84
- page_icon="🦙",
85
- layout="wide",
86
- initial_sidebar_state="expanded"
87
- )
88
-
89
- # Header
90
- st.title("🦙 CapybaraHermes-2.5-Mistral-7B")
91
- st.markdown("*7B parameter model with OpenAI-compatible API*")
92
-
93
- # Get current space URL for examples
94
- space_url = st.text_input(
95
- "🌐 Your Space URL",
96
- placeholder="https://your-username-your-space.hf.space",
97
- help="Enter your actual Hugging Face Space URL for API examples"
98
- )
99
-
100
- # Check API health
101
- api_healthy = check_api_health()
102
-
103
- col1, col2, col3 = st.columns([2, 1, 1])
104
- with col1:
105
- if api_healthy:
106
- st.success("✅ API Server is running")
107
- else:
108
- st.warning("⏳ API Server is starting... (2-3 minutes)")
109
-
110
- with col2:
111
- if st.button("🔄 Refresh"):
112
- st.rerun()
113
-
114
- with col3:
115
- if st.button("📊 API Info"):
116
- info = get_api_info()
117
- if info:
118
- st.json(info)
119
-
120
- if not api_healthy:
121
- st.info("🕐 The model is loading in the background. First startup takes 2-3 minutes.")
122
- st.stop()
123
-
124
- # Sidebar
125
- with st.sidebar:
126
- st.header("⚙️ Settings")
127
-
128
- # API Key selection
129
- selected_api_key = st.selectbox(
130
- "🔑 API Key",
131
- API_KEYS,
132
- index=0,
133
- help="Choose an API key for authentication"
134
- )
135
-
136
- # Model parameters
137
- max_tokens = st.slider("Max Tokens", 50, 1024, 512)
138
- temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
139
-
140
- st.divider()
141
-
142
- # API Information
143
- st.header("📡 API Access")
144
- if space_url:
145
- st.markdown(f"**Base URL:** `{space_url}/v1`")
146
- else:
147
- st.markdown("**Base URL:** `https://your-space.hf.space/v1`")
148
-
149
- st.markdown("**Endpoints:**")
150
- st.code("/v1/models", language="text")
151
- st.code("/v1/chat/completions", language="text")
152
- st.code("/v1/health", language="text")
153
- st.code("/v1/docs", language="text")
154
-
155
- st.markdown("**Authentication:**")
156
- st.code(f"Authorization: Bearer {selected_api_key}", language="text")
157
-
158
- st.divider()
159
-
160
- # Performance info
161
- st.header("📈 Performance")
162
- st.metric("Expected Speed", "2-8 tok/s")
163
- st.metric("Context Length", "4,096 tokens")
164
- st.metric("Model Size", "5GB")
165
-
166
- # Main content tabs
167
- tab1, tab2, tab3 = st.tabs(["💬 Chat", "🔗 API Usage", "🧪 API Test"])
168
-
169
- with tab1:
170
- # Chat interface
171
- if "messages" not in st.session_state:
172
- st.session_state.messages = []
173
-
174
- # Display chat history
175
- for message in st.session_state.messages:
176
- with st.chat_message(message["role"]):
177
- st.markdown(message["content"])
178
- if message["role"] == "assistant" and "stats" in message:
179
- st.caption(message["stats"])
180
-
181
- # Chat input
182
- if prompt := st.chat_input("Ask me anything..."):
183
- # Add user message
184
- st.session_state.messages.append({"role": "user", "content": prompt})
185
- with st.chat_message("user"):
186
- st.markdown(prompt)
187
-
188
- # Generate response
189
- with st.chat_message("assistant"):
190
- with st.spinner("🤔 Thinking..."):
191
- # Prepare messages for API
192
- api_messages = [
193
- {"role": msg["role"], "content": msg["content"]}
194
- for msg in st.session_state.messages
195
- if "stats" not in msg
196
- ]
197
-
198
- result = call_api(api_messages, max_tokens, temperature, selected_api_key)
199
-
200
- if result and 'choices' in result:
201
- response = result['choices'][0]['message']['content']
202
- usage = result.get('usage', {})
203
-
204
- completion_tokens = usage.get('completion_tokens', 0)
205
- generation_time = usage.get('total_tokens', 0) / 4.0 # Rough estimate
206
- stats = f"📊 {completion_tokens} tokens • ~{generation_time:.1f}s • API: {selected_api_key[:12]}..."
207
-
208
- st.markdown(response)
209
- st.caption(stats)
210
-
211
- st.session_state.messages.append({
212
- "role": "assistant",
213
- "content": response,
214
- "stats": stats
215
- })
216
-
217
- # Controls
218
- col1, col2 = st.columns([1, 1])
219
- with col1:
220
- if st.button("🗑️ Clear Chat", type="secondary"):
221
- st.session_state.messages = []
222
- st.rerun()
223
- with col2:
224
- if st.button("💾 Export Chat", type="secondary"):
225
- chat_json = json.dumps(st.session_state.messages, indent=2)
226
- st.download_button(
227
- "📥 Download JSON",
228
- chat_json,
229
- "chat_export.json",
230
- "application/json"
231
- )
232
-
233
- with tab2:
234
- st.header("🔗 API Usage Examples")
235
-
236
- # Determine base URL
237
- base_url = space_url + "/v1" if space_url else "https://your-username-your-space.hf.space/v1"
238
-
239
- st.subheader("🐍 Python (OpenAI SDK)")
240
- python_code = f"""# Install OpenAI SDK
241
- pip install openai
242
-
243
- # Usage example
244
- from openai import OpenAI
245
-
246
- client = OpenAI(
247
- base_url="{base_url}",
248
- api_key="{selected_api_key}"
249
- )
250
-
251
- response = client.chat.completions.create(
252
- model="capybarahermes-2.5-mistral-7b",
253
- messages=[
254
- {{"role": "user", "content": "Explain quantum computing"}}
255
- ],
256
- max_tokens=512,
257
- temperature=0.7
258
- )
259
-
260
- print(response.choices[0].message.content)
261
- print(f"Tokens used: {{response.usage.total_tokens}}")"""
262
-
263
- st.code(python_code, language="python")
264
-
265
- st.subheader("🌐 JavaScript/Node.js")
266
- js_code = f"""// Install: npm install openai
267
- import OpenAI from 'openai';
268
-
269
- const client = new OpenAI({{
270
- baseURL: '{base_url}',
271
- apiKey: '{selected_api_key}'
272
- }});
273
-
274
- const response = await client.chat.completions.create({{
275
- model: 'capybarahermes-2.5-mistral-7b',
276
- messages: [{{ role: 'user', content: 'Hello world!' }}],
277
- max_tokens: 300
278
- }});
279
-
280
- console.log(response.choices[0].message.content);"""
281
-
282
- st.code(js_code, language="javascript")
283
-
284
- st.subheader("🔧 cURL")
285
- curl_code = f"""# Chat completion
286
- curl -X POST "{base_url}/chat/completions" \\
287
- -H "Content-Type: application/json" \\
288
- -H "Authorization: Bearer {selected_api_key}" \\
289
- -d '{{
290
- "model": "capybarahermes-2.5-mistral-7b",
291
- "messages": [{{"role": "user", "content": "Hello!"}}],
292
- "max_tokens": 200,
293
- "temperature": 0.7
294
- }}'
295
-
296
- # List models
297
- curl -H "Authorization: Bearer {selected_api_key}" \\
298
- "{base_url}/models"
299
-
300
- # Health check (no auth required)
301
- curl "{base_url}/health\""""
302
-
303
- st.code(curl_code, language="bash")
304
-
305
- st.subheader("🔑 API Authentication")
306
- st.info(f"""
307
- **Available API Keys:**
308
- - `sk-adminkey02` (admin access)
309
- - `sk-testkey123` (testing)
310
- - `sk-userkey456` (user access)
311
- - `sk-demokey789` (demo access)
312
-
313
- **Currently selected:** `{selected_api_key}`
314
- """)
315
-
316
- with tab3:
317
- st.header("🧪 API Testing Interface")
318
-
319
- col1, col2 = st.columns(2)
320
-
321
- with col1:
322
- st.subheader("Test Chat Completion")
323
- test_message = st.text_area("Test Message", "What is artificial intelligence?")
324
- test_max_tokens = st.number_input("Max Tokens", 50, 1000, 200)
325
- test_temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1, key="test_temp")
326
- test_api_key = st.selectbox("API Key", API_KEYS, key="test_key")
327
-
328
- if st.button("🧪 Test API Call"):
329
- with st.spinner("Testing..."):
330
- result = call_api(
331
- [{"role": "user", "content": test_message}],
332
- test_max_tokens,
333
- test_temperature,
334
- test_api_key
335
- )
336
- if result:
337
- st.success("✅ API Response:")
338
- st.json(result)
339
-
340
- with col2:
341
- st.subheader("Health & Models")
342
-
343
- if st.button("🏥 Check Health"):
344
- try:
345
- response = requests.get("http://localhost:8000/v1/health")
346
- if response.status_code == 200:
347
- st.success("✅ API is healthy")
348
- st.json(response.json())
349
- else:
350
- st.error(f"❌ Health check failed: {response.status_code}")
351
- except Exception as e:
352
- st.error(f"❌ Connection failed: {e}")
353
-
354
- if st.button("📋 List Models"):
355
- try:
356
- response = requests.get(
357
- "http://localhost:8000/v1/models",
358
- headers={"Authorization": f"Bearer {selected_api_key}"}
359
- )
360
- if response.status_code == 200:
361
- st.success("✅ Models:")
362
- st.json(response.json())
363
- else:
364
- st.error(f"❌ Request failed: {response.status_code}")
365
- except Exception as e:
366
- st.error(f"❌ Connection failed: {e}")
367
-
368
- st.subheader("🔗 Quick Links")
369
- if space_url:
370
- st.markdown(f"- [API Docs]({space_url}/v1/docs)")
371
- st.markdown(f"- [Health Check]({space_url}/v1/health)")
372
- st.markdown(f"- [Models]({space_url}/v1/models)")
373
-
374
- if __name__ == "__main__":
375
- main()