Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# ================== YOUR SETTINGS ==================
|
| 6 |
API_KEY = os.environ.get("API_KEY")
|
|
@@ -9,6 +12,10 @@ client = OpenAI(
|
|
| 9 |
api_key=API_KEY
|
| 10 |
)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Auto-fetch available models
|
| 13 |
print("Fetching available models...")
|
| 14 |
try:
|
|
@@ -30,7 +37,7 @@ def is_model_available(model_id):
|
|
| 30 |
try:
|
| 31 |
test_completion = client.chat.completions.create(
|
| 32 |
model=model_id,
|
| 33 |
-
messages=[{"role": "user", "content": "Say 'test'"}],
|
| 34 |
max_tokens=16384,
|
| 35 |
stream=False
|
| 36 |
)
|
|
@@ -45,9 +52,117 @@ if not available_models:
|
|
| 45 |
available_models = [default_model] # Fallback to default model if no models are available
|
| 46 |
default_model = available_models[0] if available_models else None
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# ================== GUI ==================
|
| 49 |
with gr.Blocks(title="NVIDIA AI Chat") as demo:
|
| 50 |
-
gr.Markdown("# 🟢 NVIDIA AI Chat\nFixed format errors + manual load with test.")
|
| 51 |
|
| 52 |
current_model = gr.State(None)
|
| 53 |
status_label = gr.Markdown("⚠️ No model loaded.")
|
|
@@ -80,7 +195,17 @@ with gr.Blocks(title="NVIDIA AI Chat") as demo:
|
|
| 80 |
load_btn.click(load_model, model_dropdown, [current_model, status_label])
|
| 81 |
|
| 82 |
# Send (Enter or button)
|
| 83 |
-
msg.submit(chat_stream, [msg, chatbot, current_model], chatbot).then(lambda: "", None, msg)
|
| 84 |
-
send_btn.click(chat_stream, [msg, chatbot, current_model], chatbot).then(lambda: "", None, msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
demo.launch(auth=("bsis3b", "classmate"))
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
from openai import OpenAI
|
| 4 |
import gradio as gr
|
| 5 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
+
from huggingface_hub import HfApi, HfFolder
|
| 7 |
|
| 8 |
# ================== YOUR SETTINGS ==================
|
| 9 |
API_KEY = os.environ.get("API_KEY")
|
|
|
|
| 12 |
api_key=API_KEY
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Hugging Face API
|
| 16 |
+
hf_api = HfApi()
|
| 17 |
+
hf_token = HfFolder.get_token()
|
| 18 |
+
|
| 19 |
# Auto-fetch available models
|
| 20 |
print("Fetching available models...")
|
| 21 |
try:
|
|
|
|
| 37 |
try:
|
| 38 |
test_completion = client.chat.completions.create(
|
| 39 |
model=model_id,
|
| 40 |
+
messages=[{"role": "user", "content": "Say \'test\'"}],
|
| 41 |
max_tokens=16384,
|
| 42 |
stream=False
|
| 43 |
)
|
|
|
|
| 52 |
available_models = [default_model] # Fallback to default model if no models are available
|
| 53 |
default_model = available_models[0] if available_models else None
|
| 54 |
|
| 55 |
+
# User data storage
|
| 56 |
+
def register_user(username, password):
|
| 57 |
+
try:
|
| 58 |
+
user_data = {
|
| 59 |
+
"password": generate_password_hash(password),
|
| 60 |
+
"history": []
|
| 61 |
+
}
|
| 62 |
+
hf_api.upload_file(
|
| 63 |
+
path_or_fileobj=json.dumps(user_data),
|
| 64 |
+
path_in_repo=f"{username}.json",
|
| 65 |
+
repo_id="your-repo-id",
|
| 66 |
+
repo_type="dataset",
|
| 67 |
+
token=hf_token
|
| 68 |
+
)
|
| 69 |
+
return "Registration successful!"
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Registration failed: {e}"
|
| 72 |
+
|
| 73 |
+
def login_user(username, password):
|
| 74 |
+
try:
|
| 75 |
+
user_data = hf_api.download_file(
|
| 76 |
+
repo_id="your-repo-id",
|
| 77 |
+
repo_type="dataset",
|
| 78 |
+
path_in_repo=f"{username}.json",
|
| 79 |
+
token=hf_token
|
| 80 |
+
)
|
| 81 |
+
with open(user_data, 'r') as f:
|
| 82 |
+
data = json.load(f)
|
| 83 |
+
if check_password_hash(data["password"], password):
|
| 84 |
+
return data["history"], "Login successful!"
|
| 85 |
+
else:
|
| 86 |
+
return None, "Incorrect password."
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return None, f"Login failed: {e}"
|
| 89 |
+
|
| 90 |
+
def save_history(username, history):
|
| 91 |
+
try:
|
| 92 |
+
user_data = hf_api.download_file(
|
| 93 |
+
repo_id="your-repo-id",
|
| 94 |
+
repo_type="dataset",
|
| 95 |
+
path_in_repo=f"{username}.json",
|
| 96 |
+
token=hf_token
|
| 97 |
+
)
|
| 98 |
+
with open(user_data, 'r') as f:
|
| 99 |
+
data = json.load(f)
|
| 100 |
+
data["history"] = history
|
| 101 |
+
hf_api.upload_file(
|
| 102 |
+
path_or_fileobj=json.dumps(data),
|
| 103 |
+
path_in_repo=f"{username}.json",
|
| 104 |
+
repo_id="your-repo-id",
|
| 105 |
+
repo_type="dataset",
|
| 106 |
+
token=hf_token
|
| 107 |
+
)
|
| 108 |
+
return "History saved!"
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"Failed to save history: {e}"
|
| 111 |
+
|
| 112 |
+
def load_model(selected_model):
|
| 113 |
+
try:
|
| 114 |
+
test_completion = client.chat.completions.create(
|
| 115 |
+
model=selected_model,
|
| 116 |
+
messages=[{"role": "user", "content": "Say \'test\'"}],
|
| 117 |
+
max_tokens=16384,
|
| 118 |
+
stream=False
|
| 119 |
+
)
|
| 120 |
+
if test_completion.choices[0].message.content:
|
| 121 |
+
return selected_model, f"✅ Loaded & Ready: {selected_model}"
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return None, f"❌ Unavailable: {str(e)}\\nTry another model or NVIDIA support."
|
| 124 |
+
|
| 125 |
+
def chat_stream(message, history, current_model, username):
|
| 126 |
+
if not current_model:
|
| 127 |
+
history.append({"role": "assistant", "content": "⚠️ Load a model first!"})
|
| 128 |
+
return history
|
| 129 |
+
|
| 130 |
+
history.append({"role": "user", "content": message})
|
| 131 |
+
yield history
|
| 132 |
+
|
| 133 |
+
history.append({"role": "assistant", "content": ""})
|
| 134 |
+
yield history
|
| 135 |
+
|
| 136 |
+
messages = []
|
| 137 |
+
for msg in history[:-1]:
|
| 138 |
+
content = msg["content"] if isinstance(msg["content"], str) else str(msg["content"])
|
| 139 |
+
messages.append({"role": msg["role"], "content": content})
|
| 140 |
+
|
| 141 |
+
full_response = ""
|
| 142 |
+
try:
|
| 143 |
+
stream = client.chat.completions.create(
|
| 144 |
+
model=current_model,
|
| 145 |
+
messages=messages,
|
| 146 |
+
temperature=0.7,
|
| 147 |
+
top_p=0.8,
|
| 148 |
+
max_tokens=4096,
|
| 149 |
+
stream=True
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
for chunk in stream:
|
| 153 |
+
if chunk.choices and chunk.choices[0].delta.content is not None:
|
| 154 |
+
full_response += chunk.choices[0].delta.content
|
| 155 |
+
history[-1]["content"] = full_response
|
| 156 |
+
yield history
|
| 157 |
+
except Exception as e:
|
| 158 |
+
history[-1]["content"] = f"❌ Error: {str(e)}\\nSwitch models."
|
| 159 |
+
yield history
|
| 160 |
+
|
| 161 |
+
save_history(username, history)
|
| 162 |
+
|
| 163 |
# ================== GUI ==================
|
| 164 |
with gr.Blocks(title="NVIDIA AI Chat") as demo:
|
| 165 |
+
gr.Markdown("# 🟢 NVIDIA AI Chat\\nFixed format errors + manual load with test.")
|
| 166 |
|
| 167 |
current_model = gr.State(None)
|
| 168 |
status_label = gr.Markdown("⚠️ No model loaded.")
|
|
|
|
| 195 |
load_btn.click(load_model, model_dropdown, [current_model, status_label])
|
| 196 |
|
| 197 |
# Send (Enter or button)
|
| 198 |
+
msg.submit(chat_stream, [msg, chatbot, current_model, "username"], chatbot).then(lambda: "", None, msg)
|
| 199 |
+
send_btn.click(chat_stream, [msg, chatbot, current_model, "username"], chatbot).then(lambda: "", None, msg)
|
| 200 |
+
|
| 201 |
+
# Authentication
|
| 202 |
+
with gr.Row():
|
| 203 |
+
username = gr.Textbox(label="Username")
|
| 204 |
+
password = gr.Password(label="Password")
|
| 205 |
+
register_btn = gr.Button("Register")
|
| 206 |
+
login_btn = gr.Button("Login")
|
| 207 |
+
|
| 208 |
+
register_btn.click(register_user, [username, password], status_label)
|
| 209 |
+
login_btn.click(login_user, [username, password], [chatbot, status_label])
|
| 210 |
|
| 211 |
demo.launch(auth=("bsis3b", "classmate"))
|