Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
-
# app.py β
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import threading
|
| 6 |
import gradio as gr
|
| 7 |
-
from huggingface_hub import InferenceClient
|
| 8 |
from datasets import load_dataset
|
| 9 |
from duckduckgo_search import DDGS
|
| 10 |
|
| 11 |
-
|
| 12 |
# ---------------- CONFIG ----------------
|
| 13 |
-
MODEL_ID = "openai/gpt-oss-120b"
|
| 14 |
DATA_DIR = "/data" if os.path.isdir("/data") else "./data"
|
| 15 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 16 |
|
|
@@ -18,18 +16,92 @@ SHORT_TERM_LIMIT = 10
|
|
| 18 |
SUMMARY_MAX_TOKENS = 150
|
| 19 |
MEMORY_LOCK = threading.Lock()
|
| 20 |
|
| 21 |
-
# ----------------
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
-
ds1 = load_dataset("HuggingFaceH4/ultrachat_200k")
|
| 30 |
-
ds2 = load_dataset("Anthropic/hh-rlhf")
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def get_user_id(hf_token: gr.OAuthToken | None):
|
| 34 |
if hf_token and getattr(hf_token, "token", None):
|
| 35 |
return "user_" + hf_token.token[:12]
|
|
@@ -59,7 +131,7 @@ def save_memory(user_id: str, memory: dict):
|
|
| 59 |
except Exception as e:
|
| 60 |
print("save_memory error:", e)
|
| 61 |
|
| 62 |
-
# ----------------
|
| 63 |
def normalize_history(history):
|
| 64 |
out = []
|
| 65 |
if not history: return out
|
|
@@ -74,7 +146,7 @@ def normalize_history(history):
|
|
| 74 |
out.append({"role": "user", "content": turn})
|
| 75 |
return out
|
| 76 |
|
| 77 |
-
# ----------------
|
| 78 |
def _get_chat_response_sync(client: InferenceClient, messages, max_tokens=SUMMARY_MAX_TOKENS, temperature=0.3, top_p=0.9):
|
| 79 |
try:
|
| 80 |
resp = client.chat_completion(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, stream=False)
|
|
@@ -94,7 +166,7 @@ def _get_chat_response_sync(client: InferenceClient, messages, max_tokens=SUMMAR
|
|
| 94 |
pass
|
| 95 |
return ""
|
| 96 |
|
| 97 |
-
# ----------------
|
| 98 |
def web_search(query, num_results=3):
|
| 99 |
try:
|
| 100 |
with DDGS() as ddgs:
|
|
@@ -109,14 +181,14 @@ def web_search(query, num_results=3):
|
|
| 109 |
except Exception as e:
|
| 110 |
return f"β Search error: {str(e)}"
|
| 111 |
|
| 112 |
-
# ----------------
|
| 113 |
def summarize_old_messages(client: InferenceClient, old_messages):
|
| 114 |
text = "\n".join([f"{m['role']}: {m['content']}" for m in old_messages])
|
| 115 |
system = {"role": "system", "content": "You are a summarizer. Summarize <=150 words."}
|
| 116 |
user = {"role": "user", "content": text}
|
| 117 |
return _get_chat_response_sync(client, [system, user])
|
| 118 |
|
| 119 |
-
# ----------------
|
| 120 |
def show_memory(hf_token: gr.OAuthToken | None = None):
|
| 121 |
user = get_user_id(hf_token)
|
| 122 |
p = memory_file_path(user)
|
|
@@ -133,9 +205,10 @@ def clear_memory(hf_token: gr.OAuthToken | None = None):
|
|
| 133 |
return f"β
Memory cleared for {user}"
|
| 134 |
return "βΉοΈ No memory to clear."
|
| 135 |
|
| 136 |
-
# ----------------
|
| 137 |
def respond(message, history: list, system_message, max_tokens, temperature, top_p,
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
client = InferenceClient(token=(hf_token.token if hf_token else None), model=MODEL_ID)
|
| 141 |
user_id = get_user_id(hf_token)
|
|
@@ -144,6 +217,7 @@ def respond(message, history: list, system_message, max_tokens, temperature, top
|
|
| 144 |
session_history = normalize_history(history)
|
| 145 |
combined = memory.get("short_term", []) + session_history
|
| 146 |
|
|
|
|
| 147 |
if len(combined) > SHORT_TERM_LIMIT:
|
| 148 |
to_summarize = combined[:len(combined) - SHORT_TERM_LIMIT]
|
| 149 |
summary = summarize_old_messages(client, to_summarize)
|
|
@@ -156,15 +230,39 @@ def respond(message, history: list, system_message, max_tokens, temperature, top
|
|
| 156 |
if enable_persistent_memory:
|
| 157 |
save_memory(user_id, memory)
|
| 158 |
|
|
|
|
| 159 |
messages = [{"role": "system", "content": system_message}]
|
| 160 |
if memory.get("long_term"):
|
| 161 |
messages.append({"role": "system", "content": "Long-term memory:\n" + memory["long_term"]})
|
| 162 |
-
messages.extend(memory["short_term"])
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
|
|
|
| 168 |
response = ""
|
| 169 |
try:
|
| 170 |
for chunk in client.chat_completion(messages, max_tokens=int(max_tokens),
|
|
@@ -189,33 +287,65 @@ def respond(message, history: list, system_message, max_tokens, temperature, top
|
|
| 189 |
yield f"β οΈ Inference error: {e}"
|
| 190 |
return
|
| 191 |
|
|
|
|
| 192 |
memory["short_term"].append({"role": "assistant", "content": response})
|
| 193 |
memory["short_term"] = memory["short_term"][-SHORT_TERM_LIMIT:]
|
| 194 |
if enable_persistent_memory:
|
| 195 |
save_memory(user_id, memory)
|
| 196 |
|
| 197 |
-
# ----------------
|
| 198 |
chatbot = gr.ChatInterface(
|
| 199 |
respond,
|
| 200 |
type="messages",
|
| 201 |
additional_inputs=[
|
| 202 |
-
gr.Textbox(value="You are
|
| 203 |
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
| 204 |
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 205 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 206 |
-
gr.Checkbox(value=True, label="Enable Web Search
|
| 207 |
-
gr.Checkbox(value=True, label="Enable
|
|
|
|
|
|
|
| 208 |
],
|
| 209 |
)
|
| 210 |
|
| 211 |
-
with gr.Blocks(title="AI Chatbot
|
| 212 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
with gr.Sidebar():
|
| 214 |
gr.LoginButton()
|
| 215 |
-
gr.Markdown("
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
chatbot.render()
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py β Enhanced version with streaming datasets + memory + web search
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import threading
|
| 5 |
import gradio as gr
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
from datasets import load_dataset
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
|
|
|
|
| 10 |
# ---------------- CONFIG ----------------
|
| 11 |
+
MODEL_ID = "openai/gpt-oss-120b"
|
| 12 |
DATA_DIR = "/data" if os.path.isdir("/data") else "./data"
|
| 13 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 14 |
|
|
|
|
| 16 |
SUMMARY_MAX_TOKENS = 150
|
| 17 |
MEMORY_LOCK = threading.Lock()
|
| 18 |
|
| 19 |
+
# ---------------- STREAMING DATASET LOADING (ZERO STORAGE!) ----------------
|
| 20 |
+
print("π Loading datasets in streaming mode...")
|
| 21 |
+
|
| 22 |
+
# FineWeb 100BT - Full access via streaming!
|
| 23 |
+
fineweb_stream = load_dataset(
|
| 24 |
+
"HuggingFaceFW/fineweb",
|
| 25 |
+
split="train", # Access to full dataset including 100BT
|
| 26 |
+
streaming=True # No local storage used!
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Other datasets in streaming mode
|
| 30 |
+
ultrachat_stream = load_dataset(
|
| 31 |
+
"HuggingFaceH4/ultrachat_200k",
|
| 32 |
+
split="train",
|
| 33 |
+
streaming=True
|
| 34 |
)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
hh_rlhf_stream = load_dataset(
|
| 37 |
+
"Anthropic/hh-rlhf",
|
| 38 |
+
split="train",
|
| 39 |
+
streaming=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
print("β
All datasets loaded in streaming mode - 0GB storage used!")
|
| 43 |
+
|
| 44 |
+
# ---------------- DATASET SEARCH FUNCTIONS ----------------
|
| 45 |
+
def search_fineweb_knowledge(query, max_samples=5, max_search=2000):
|
| 46 |
+
"""Search through streaming FineWeb 100BT for relevant content"""
|
| 47 |
+
try:
|
| 48 |
+
relevant_texts = []
|
| 49 |
+
processed = 0
|
| 50 |
+
query_words = query.lower().split()
|
| 51 |
+
|
| 52 |
+
# Stream through FineWeb looking for relevant content
|
| 53 |
+
for sample in fineweb_stream:
|
| 54 |
+
if processed >= max_search or len(relevant_texts) >= max_samples:
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
text = sample.get('text', '').lower()
|
| 58 |
+
# Check if query words appear in text
|
| 59 |
+
if any(word in text for word in query_words):
|
| 60 |
+
content = sample['text'][:400] + "..." if len(sample['text']) > 400 else sample['text']
|
| 61 |
+
relevant_texts.append(content)
|
| 62 |
+
|
| 63 |
+
processed += 1
|
| 64 |
+
|
| 65 |
+
if relevant_texts:
|
| 66 |
+
return "π FineWeb 100BT Knowledge:\n\n" + "\n---\n".join(relevant_texts)
|
| 67 |
+
return "No relevant FineWeb content found."
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"FineWeb search error: {str(e)}"
|
| 71 |
+
|
| 72 |
+
def search_conversation_patterns(query, max_samples=3):
|
| 73 |
+
"""Search UltraChat for conversation patterns"""
|
| 74 |
+
try:
|
| 75 |
+
relevant_convos = []
|
| 76 |
+
processed = 0
|
| 77 |
+
|
| 78 |
+
for sample in ultrachat_stream:
|
| 79 |
+
if processed >= 500 or len(relevant_convos) >= max_samples:
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
# Check messages for relevance
|
| 83 |
+
messages = sample.get('messages', [])
|
| 84 |
+
for msg in messages:
|
| 85 |
+
if query.lower() in msg.get('content', '').lower():
|
| 86 |
+
relevant_convos.append({
|
| 87 |
+
'role': msg.get('role', 'unknown'),
|
| 88 |
+
'content': msg.get('content', '')[:300] + "..."
|
| 89 |
+
})
|
| 90 |
+
break
|
| 91 |
+
|
| 92 |
+
processed += 1
|
| 93 |
+
|
| 94 |
+
if relevant_convos:
|
| 95 |
+
result = "π¬ Conversation Patterns:\n\n"
|
| 96 |
+
for convo in relevant_convos:
|
| 97 |
+
result += f"**{convo['role']}**: {convo['content']}\n\n"
|
| 98 |
+
return result
|
| 99 |
+
return ""
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return f"Conversation search error: {str(e)}"
|
| 103 |
+
|
| 104 |
+
# ---------------- HELPERS: MEMORY ----------------
|
| 105 |
def get_user_id(hf_token: gr.OAuthToken | None):
|
| 106 |
if hf_token and getattr(hf_token, "token", None):
|
| 107 |
return "user_" + hf_token.token[:12]
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
print("save_memory error:", e)
|
| 133 |
|
| 134 |
+
# ---------------- NORMALIZE HISTORY ----------------
|
| 135 |
def normalize_history(history):
|
| 136 |
out = []
|
| 137 |
if not history: return out
|
|
|
|
| 146 |
out.append({"role": "user", "content": turn})
|
| 147 |
return out
|
| 148 |
|
| 149 |
+
# ---------------- SYNC COMPLETION ----------------
|
| 150 |
def _get_chat_response_sync(client: InferenceClient, messages, max_tokens=SUMMARY_MAX_TOKENS, temperature=0.3, top_p=0.9):
|
| 151 |
try:
|
| 152 |
resp = client.chat_completion(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, stream=False)
|
|
|
|
| 166 |
pass
|
| 167 |
return ""
|
| 168 |
|
| 169 |
+
# ---------------- WEB SEARCH ----------------
|
| 170 |
def web_search(query, num_results=3):
|
| 171 |
try:
|
| 172 |
with DDGS() as ddgs:
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
return f"β Search error: {str(e)}"
|
| 183 |
|
| 184 |
+
# ---------------- SUMMARIZATION ----------------
|
| 185 |
def summarize_old_messages(client: InferenceClient, old_messages):
|
| 186 |
text = "\n".join([f"{m['role']}: {m['content']}" for m in old_messages])
|
| 187 |
system = {"role": "system", "content": "You are a summarizer. Summarize <=150 words."}
|
| 188 |
user = {"role": "user", "content": text}
|
| 189 |
return _get_chat_response_sync(client, [system, user])
|
| 190 |
|
| 191 |
+
# ---------------- MEMORY TOOLS ----------------
|
| 192 |
def show_memory(hf_token: gr.OAuthToken | None = None):
|
| 193 |
user = get_user_id(hf_token)
|
| 194 |
p = memory_file_path(user)
|
|
|
|
| 205 |
return f"β
Memory cleared for {user}"
|
| 206 |
return "βΉοΈ No memory to clear."
|
| 207 |
|
| 208 |
+
# ---------------- MAIN CHAT WITH ENHANCED CAPABILITIES ----------------
|
| 209 |
def respond(message, history: list, system_message, max_tokens, temperature, top_p,
|
| 210 |
+
enable_web_search, enable_fineweb_search, enable_conversation_search,
|
| 211 |
+
enable_persistent_memory, hf_token: gr.OAuthToken = None):
|
| 212 |
|
| 213 |
client = InferenceClient(token=(hf_token.token if hf_token else None), model=MODEL_ID)
|
| 214 |
user_id = get_user_id(hf_token)
|
|
|
|
| 217 |
session_history = normalize_history(history)
|
| 218 |
combined = memory.get("short_term", []) + session_history
|
| 219 |
|
| 220 |
+
# Memory management
|
| 221 |
if len(combined) > SHORT_TERM_LIMIT:
|
| 222 |
to_summarize = combined[:len(combined) - SHORT_TERM_LIMIT]
|
| 223 |
summary = summarize_old_messages(client, to_summarize)
|
|
|
|
| 230 |
if enable_persistent_memory:
|
| 231 |
save_memory(user_id, memory)
|
| 232 |
|
| 233 |
+
# Build context
|
| 234 |
messages = [{"role": "system", "content": system_message}]
|
| 235 |
if memory.get("long_term"):
|
| 236 |
messages.append({"role": "system", "content": "Long-term memory:\n" + memory["long_term"]})
|
|
|
|
| 237 |
|
| 238 |
+
# Enhanced search capabilities
|
| 239 |
+
context_parts = []
|
| 240 |
+
|
| 241 |
+
# Web search
|
| 242 |
+
if enable_web_search and any(k in message.lower() for k in ["search", "google", "tin tα»©c", "news", "what is", "latest", "current"]):
|
| 243 |
+
web_results = web_search(message)
|
| 244 |
+
context_parts.append(web_results)
|
| 245 |
+
|
| 246 |
+
# FineWeb 100BT search
|
| 247 |
+
if enable_fineweb_search:
|
| 248 |
+
fineweb_results = search_fineweb_knowledge(message)
|
| 249 |
+
if "No relevant FineWeb" not in fineweb_results:
|
| 250 |
+
context_parts.append(fineweb_results)
|
| 251 |
+
|
| 252 |
+
# Conversation pattern search
|
| 253 |
+
if enable_conversation_search:
|
| 254 |
+
convo_results = search_conversation_patterns(message)
|
| 255 |
+
if convo_results:
|
| 256 |
+
context_parts.append(convo_results)
|
| 257 |
+
|
| 258 |
+
# Add enhanced context
|
| 259 |
+
if context_parts:
|
| 260 |
+
enhanced_context = "\n\n".join(context_parts)
|
| 261 |
+
messages.append({"role": "system", "content": f"Additional Context:\n{enhanced_context}"})
|
| 262 |
+
|
| 263 |
+
messages.extend(memory["short_term"])
|
| 264 |
|
| 265 |
+
# Generate response
|
| 266 |
response = ""
|
| 267 |
try:
|
| 268 |
for chunk in client.chat_completion(messages, max_tokens=int(max_tokens),
|
|
|
|
| 287 |
yield f"β οΈ Inference error: {e}"
|
| 288 |
return
|
| 289 |
|
| 290 |
+
# Update memory
|
| 291 |
memory["short_term"].append({"role": "assistant", "content": response})
|
| 292 |
memory["short_term"] = memory["short_term"][-SHORT_TERM_LIMIT:]
|
| 293 |
if enable_persistent_memory:
|
| 294 |
save_memory(user_id, memory)
|
| 295 |
|
| 296 |
+
# ---------------- ENHANCED GRADIO UI ----------------
|
| 297 |
chatbot = gr.ChatInterface(
|
| 298 |
respond,
|
| 299 |
type="messages",
|
| 300 |
additional_inputs=[
|
| 301 |
+
gr.Textbox(value="You are an advanced AI assistant with access to web search, FineWeb 100BT knowledge, conversation patterns, and persistent memory. Provide comprehensive, accurate responses.", label="System message"),
|
| 302 |
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
| 303 |
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 304 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 305 |
+
gr.Checkbox(value=True, label="π Enable Web Search"),
|
| 306 |
+
gr.Checkbox(value=True, label="π Enable FineWeb 100BT Search"),
|
| 307 |
+
gr.Checkbox(value=True, label="π¬ Enable Conversation Pattern Search"),
|
| 308 |
+
gr.Checkbox(value=True, label="π§ Enable Persistent Memory"),
|
| 309 |
],
|
| 310 |
)
|
| 311 |
|
| 312 |
+
with gr.Blocks(title="Enhanced AI Chatbot - FineWeb 100BT") as demo:
|
| 313 |
+
gr.Markdown("""
|
| 314 |
+
# π Enhanced AI Chatbot with FineWeb 100BT Streaming
|
| 315 |
+
|
| 316 |
+
**Now with access to 100+ billion tokens via streaming - Zero storage used!**
|
| 317 |
+
|
| 318 |
+
## π₯ Features:
|
| 319 |
+
- **π FineWeb 100BT**: Full access to 100+ billion token web dataset
|
| 320 |
+
- **π Web Search**: Real-time internet information
|
| 321 |
+
- **π¬ Conversation Patterns**: Learn from 200k+ high-quality conversations
|
| 322 |
+
- **π§ Persistent Memory**: Remembers across sessions
|
| 323 |
+
- **β‘ Zero Storage**: All datasets stream on-demand
|
| 324 |
+
- **π° Cost**: $0.00 (still free!)
|
| 325 |
+
""")
|
| 326 |
+
|
| 327 |
with gr.Sidebar():
|
| 328 |
gr.LoginButton()
|
| 329 |
+
gr.Markdown("""
|
| 330 |
+
### π Dataset Access:
|
| 331 |
+
- **FineWeb**: 100BT tokens (streaming)
|
| 332 |
+
- **UltraChat**: 515k conversations (streaming)
|
| 333 |
+
- **HH-RLHF**: 169k samples (streaming)
|
| 334 |
+
- **Storage Used**: 0GB π
|
| 335 |
+
|
| 336 |
+
### π§ Memory Tools:
|
| 337 |
+
""")
|
| 338 |
+
|
| 339 |
+
with gr.Row():
|
| 340 |
+
show_btn = gr.Button("π Show Memory", size="sm")
|
| 341 |
+
clear_btn = gr.Button("ποΈ Clear Memory", size="sm")
|
| 342 |
+
|
| 343 |
+
memory_output = gr.Textbox(label="Memory Status", lines=10, max_lines=15)
|
| 344 |
+
|
| 345 |
+
show_btn.click(show_memory, inputs=None, outputs=memory_output)
|
| 346 |
+
clear_btn.click(clear_memory, inputs=None, outputs=memory_output)
|
| 347 |
+
|
| 348 |
chatbot.render()
|
| 349 |
|
| 350 |
if __name__ == "__main__":
|
| 351 |
+
demo.launch()
|