owlninjam commited on
Commit
bde8af9
·
verified ·
1 Parent(s): 526f112

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -200
app.py DELETED
@@ -1,200 +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
- API_KEYS = ["sk-adminkey02", "sk-testkey123", "sk-userkey456", "sk-demokey789"]
12
- DEFAULT_API_KEY = "sk-adminkey02"
13
-
14
- def start_fastapi():
15
- try:
16
- subprocess.Popen([
17
- sys.executable, "-m", "uvicorn", "api:app",
18
- "--host", "0.0.0.0",
19
- "--port", "8000",
20
- "--workers", "1"
21
- ])
22
- print("🚀 FastAPI server starting on port 8000...")
23
- except Exception as e:
24
- st.error(f"Failed to start API server: {e}")
25
-
26
- if 'api_started' not in st.session_state:
27
- st.session_state.api_started = True
28
- threading.Thread(target=start_fastapi, daemon=True).start()
29
- time.sleep(8)
30
-
31
- def call_api(messages: List[Dict], max_tokens: int = 512, temperature: float = 0.7, api_key: str = DEFAULT_API_KEY):
32
- try:
33
- response = requests.post(
34
- "http://localhost:8000/v1/chat/completions",
35
- headers={"Authorization": f"Bearer {api_key}"},
36
- json={
37
- "model": "zephyr-quiklang-3b-4k",
38
- "messages": messages,
39
- "max_tokens": max_tokens,
40
- "temperature": temperature
41
- },
42
- timeout=120
43
- )
44
-
45
- if response.status_code == 200:
46
- return response.json()
47
- elif response.status_code == 401:
48
- st.error("❌ Invalid API key")
49
- return None
50
- else:
51
- st.error(f"API Error: {response.status_code} - {response.text}")
52
- return None
53
-
54
- except requests.exceptions.RequestException as e:
55
- st.error(f"Failed to connect to API: {e}")
56
- return None
57
-
58
- def check_api_health():
59
- try:
60
- response = requests.get("http://localhost:8000/v1/health", timeout=10)
61
- return response.status_code == 200
62
- except:
63
- return False
64
-
65
- def get_api_info():
66
- try:
67
- response = requests.get("http://localhost:8000/v1", timeout=5)
68
- if response.status_code == 200:
69
- return response.json()
70
- except:
71
- pass
72
- return None
73
-
74
- def main():
75
- st.set_page_config(
76
- page_title="🧠 Zephyr Quiklang 3B-4K Chat",
77
- page_icon="🧠",
78
- layout="wide",
79
- initial_sidebar_state="expanded"
80
- )
81
-
82
- st.title("🧠 Zephyr Quiklang 3B-4K")
83
- st.markdown("*3B parameter model with OpenAI-compatible API (Q4_K_M quantized)*")
84
-
85
- space_url = st.text_input(
86
- "🌐 Your Space URL",
87
- placeholder="https://your-username-your-space.hf.space",
88
- help="Enter your Hugging Face Space URL for API examples"
89
- )
90
-
91
- api_healthy = check_api_health()
92
-
93
- col1, col2, col3 = st.columns([2, 1, 1])
94
- with col1:
95
- if api_healthy:
96
- st.success("✅ API Server is running")
97
- else:
98
- st.warning("⏳ API Server is starting...")
99
-
100
- with col2:
101
- if st.button("🔄 Refresh"):
102
- st.rerun()
103
-
104
- with col3:
105
- if st.button("📊 API Info"):
106
- info = get_api_info()
107
- if info:
108
- st.json(info)
109
-
110
- if not api_healthy:
111
- st.info("🕐 The model is loading in the background. First startup may take 2-3 minutes.")
112
- st.stop()
113
-
114
- with st.sidebar:
115
- st.header("⚙️ Settings")
116
- selected_api_key = st.selectbox("🔑 API Key", API_KEYS, index=0)
117
- max_tokens = st.slider("Max Tokens", 50, 1024, 512)
118
- temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
119
-
120
- st.divider()
121
- st.header("📡 API Access")
122
- st.markdown(f"**Base URL:** `{space_url or 'https://your-space.hf.space'}/v1`")
123
- st.code("/v1/models")
124
- st.code("/v1/chat/completions")
125
- st.code("/v1/health")
126
- st.code("/v1/docs")
127
- st.markdown("**Authentication Header:**")
128
- st.code(f"Authorization: Bearer {selected_api_key}")
129
-
130
- tab1, tab2 = st.tabs(["💬 Chat", "🔗 Code Examples"])
131
-
132
- with tab1:
133
- if "messages" not in st.session_state:
134
- st.session_state.messages = []
135
-
136
- for message in st.session_state.messages:
137
- with st.chat_message(message["role"]):
138
- st.markdown(message["content"])
139
- if "stats" in message:
140
- st.caption(message["stats"])
141
-
142
- if prompt := st.chat_input("Ask me anything..."):
143
- st.session_state.messages.append({"role": "user", "content": prompt})
144
- with st.chat_message("user"):
145
- st.markdown(prompt)
146
-
147
- with st.chat_message("assistant"):
148
- with st.spinner("🤔 Thinking..."):
149
- api_messages = [
150
- {"role": msg["role"], "content": msg["content"]}
151
- for msg in st.session_state.messages if "stats" not in msg
152
- ]
153
- result = call_api(api_messages, max_tokens, temperature, selected_api_key)
154
- if result and 'choices' in result:
155
- response = result['choices'][0]['message']['content']
156
- usage = result.get('usage', {})
157
- completion_tokens = usage.get('completion_tokens', 0)
158
- generation_time = usage.get('total_tokens', 0) / 4.0
159
- stats = f"📊 {completion_tokens} tokens • ~{generation_time:.1f}s • API: {selected_api_key[:12]}..."
160
- st.markdown(response)
161
- st.caption(stats)
162
- st.session_state.messages.append({
163
- "role": "assistant",
164
- "content": response,
165
- "stats": stats
166
- })
167
-
168
- col1, col2 = st.columns(2)
169
- with col1:
170
- if st.button("🗑️ Clear Chat"):
171
- st.session_state.messages = []
172
- st.rerun()
173
- with col2:
174
- if st.button("💾 Export Chat"):
175
- chat_json = json.dumps(st.session_state.messages, indent=2)
176
- st.download_button("📥 Download JSON", chat_json, "chat_export.json", "application/json")
177
-
178
- with tab2:
179
- st.subheader("🐍 Python Example")
180
- base_url = f"{space_url or 'https://your-space.hf.space'}/v1"
181
- python_code = f"""from openai import OpenAI
182
-
183
- client = OpenAI(
184
- base_url="{base_url}",
185
- api_key="{selected_api_key}"
186
- )
187
-
188
- response = client.chat.completions.create(
189
- model="zephyr-quiklang-3b-4k",
190
- messages=[{{"role": "user", "content": "Hello!"}}],
191
- max_tokens=512,
192
- temperature=0.7
193
- )
194
-
195
- print(response.choices[0].message.content)
196
- """
197
- st.code(python_code, language="python")
198
-
199
- if __name__ == "__main__":
200
- main()