Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ✅
|
| 6 |
-
MODEL = "meta-llama/Llama-3.2-1B-Instruct"
|
| 7 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 8 |
|
| 9 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
@@ -12,9 +11,9 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 12 |
|
| 13 |
messages = [{"role": "system", "content": system_message}]
|
| 14 |
|
| 15 |
-
# History → messages
|
| 16 |
for user_msg, assistant_msg in history:
|
| 17 |
-
messages.append({"role": "user", "content": str(user_msg)})
|
| 18 |
if assistant_msg:
|
| 19 |
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 20 |
|
|
@@ -28,35 +27,43 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 28 |
temperature=temperature,
|
| 29 |
top_p=top_p
|
| 30 |
):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
yield f"❌ Lỗi: {str(e)}"
|
| 37 |
|
| 38 |
with gr.Blocks(title="NodeAI") as demo:
|
| 39 |
-
gr.Markdown("# 🚀 NodeAI -
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
gr.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
gr.Slider(1, 512, value=256, step=1, label="Max tokens"),
|
| 55 |
-
gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature"),
|
| 56 |
-
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 57 |
-
],
|
| 58 |
-
title="💬 Chat Llama 3.2"
|
| 59 |
-
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
MODEL = "microsoft/DialoGPT-large" # ✅ Model siêu ổn định
|
|
|
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
|
| 8 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
| 11 |
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
|
| 14 |
+
# History → messages (safe)
|
| 15 |
for user_msg, assistant_msg in history:
|
| 16 |
+
messages.append({"role": "user", "content": str(user_msg or "")})
|
| 17 |
if assistant_msg:
|
| 18 |
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 19 |
|
|
|
|
| 27 |
temperature=temperature,
|
| 28 |
top_p=top_p
|
| 29 |
):
|
| 30 |
+
# ✅ SAFE ACCESS - Không lỗi index
|
| 31 |
+
try:
|
| 32 |
+
if (hasattr(chunk, 'choices') and
|
| 33 |
+
len(chunk.choices) > 0 and
|
| 34 |
+
hasattr(chunk.choices[0], 'delta') and
|
| 35 |
+
hasattr(chunk.choices[0].delta, 'content') and
|
| 36 |
+
chunk.choices[0].delta.content):
|
| 37 |
+
|
| 38 |
+
content = chunk.choices[0].delta.content
|
| 39 |
+
response += content
|
| 40 |
+
yield response
|
| 41 |
+
except (IndexError, AttributeError):
|
| 42 |
+
continue # Skip chunk lỗi
|
| 43 |
+
|
| 44 |
+
# Fallback nếu không có stream
|
| 45 |
+
if not response:
|
| 46 |
+
yield "🤖 NodeAI: Xin chào! Tôi đã sẵn sàng chat."
|
| 47 |
+
|
| 48 |
except Exception as e:
|
| 49 |
yield f"❌ Lỗi: {str(e)}"
|
| 50 |
|
| 51 |
with gr.Blocks(title="NodeAI") as demo:
|
| 52 |
+
gr.Markdown("# 🚀 NodeAI - Chatbot")
|
| 53 |
|
| 54 |
+
chatbot = gr.ChatInterface(
|
| 55 |
+
respond,
|
| 56 |
+
additional_inputs=[
|
| 57 |
+
gr.Textbox(
|
| 58 |
+
value="Bạn là NodeAI - trợ lý AI tiếng Việt thân thiện.",
|
| 59 |
+
label="System prompt"
|
| 60 |
+
),
|
| 61 |
+
gr.Slider(1, 512, value=256, step=1, label="Max tokens"),
|
| 62 |
+
gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
+
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 64 |
+
],
|
| 65 |
+
title="💬 Chat ngay!"
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
|