Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,128 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient, login
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def respond(
|
8 |
-
message,
|
9 |
-
history:
|
10 |
-
system_message,
|
11 |
-
max_tokens,
|
12 |
-
temperature,
|
13 |
-
top_p,
|
14 |
-
model,
|
15 |
-
token,
|
16 |
-
):
|
17 |
"""
|
18 |
-
|
|
|
19 |
"""
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
{
|
25 |
-
"role": "system",
|
26 |
-
"content": system_message,
|
27 |
-
}
|
28 |
-
]
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
{"role": "user",
|
34 |
-
"content": val[0],
|
35 |
-
},
|
36 |
-
)
|
37 |
-
if val[1]:
|
38 |
-
messages.append(
|
39 |
-
{"role": "assistant",
|
40 |
-
"content": val[1],
|
41 |
-
}
|
42 |
-
)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
|
|
50 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
max_tokens=max_tokens,
|
55 |
-
stream=True,
|
56 |
-
temperature=temperature,
|
57 |
-
top_p=top_p,
|
58 |
-
):
|
59 |
-
token = message.choices[0].delta.content
|
60 |
-
|
61 |
-
response += token
|
62 |
-
yield response
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
67 |
-
"""
|
68 |
demo = gr.ChatInterface(
|
69 |
-
respond,
|
70 |
additional_inputs=[
|
71 |
-
gr.Textbox(
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
gr.Slider(
|
75 |
minimum=0.1,
|
76 |
maximum=1.0,
|
77 |
value=0.95,
|
78 |
step=0.05,
|
79 |
-
label="Top-p (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
),
|
81 |
-
gr.Textbox(value="Qwen/Qwen3-Coder-480B-A35B-Instruct", label="Qwen Models"),
|
82 |
-
gr.Textbox(value="HF...", label="System Api"),
|
83 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
)
|
85 |
|
86 |
-
|
87 |
if __name__ == "__main__":
|
88 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient, login
|
3 |
+
import os
|
4 |
+
from typing import List, Tuple, Optional
|
5 |
|
6 |
+
# Available models for selection
|
7 |
+
AVAILABLE_MODELS = [
|
8 |
+
"Qwen/Qwen3-Coder-480B-A35B-Instruct",
|
9 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
10 |
+
"meta-llama/Llama-3-70B-Instruct",
|
11 |
+
]
|
12 |
|
13 |
+
def initialize_client(token: str, model: str) -> Optional[InferenceClient]:
|
14 |
+
"""Initialize the InferenceClient with the provided token and model."""
|
15 |
+
try:
|
16 |
+
login(token)
|
17 |
+
return InferenceClient(model=model)
|
18 |
+
except Exception as e:
|
19 |
+
return gr.Error(f"Failed to initialize client: {str(e)}")
|
20 |
|
21 |
def respond(
|
22 |
+
message: str,
|
23 |
+
history: List[Tuple[str, str]],
|
24 |
+
system_message: str,
|
25 |
+
max_tokens: int,
|
26 |
+
temperature: float,
|
27 |
+
top_p: float,
|
28 |
+
model: str,
|
29 |
+
token: str,
|
30 |
+
) -> str:
|
31 |
"""
|
32 |
+
Generate a response using the Hugging Face Inference API.
|
33 |
+
Docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
34 |
"""
|
35 |
+
if not token:
|
36 |
+
raise gr.Error("Please provide a valid Hugging Face API token.")
|
37 |
+
if not message.strip():
|
38 |
+
raise gr.Error("Input message cannot be empty.")
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
client = initialize_client(token, model)
|
41 |
+
if isinstance(client, gr.Error):
|
42 |
+
raise client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Build message history
|
45 |
+
messages = [{"role": "system", "content": system_message}]
|
46 |
+
for user_msg, assistant_msg in history:
|
47 |
+
if user_msg:
|
48 |
+
messages.append({"role": "user", "content": user_msg})
|
49 |
+
if assistant_msg:
|
50 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
51 |
+
messages.append({"role": "user", "content": message})
|
52 |
|
53 |
+
# Generate response
|
54 |
response = ""
|
55 |
+
try:
|
56 |
+
for chunk in client.chat_completion(
|
57 |
+
messages=messages,
|
58 |
+
max_tokens=max_tokens,
|
59 |
+
stream=True,
|
60 |
+
temperature=temperature,
|
61 |
+
top_p=top_p,
|
62 |
+
):
|
63 |
+
token = chunk.choices[0].delta.content or ""
|
64 |
+
response += token
|
65 |
+
yield response
|
66 |
+
except Exception as e:
|
67 |
+
raise gr.Error(f"Error during inference: {str(e)}")
|
68 |
|
69 |
+
# Load token from environment variable for security
|
70 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# Create Gradio interface
|
|
|
|
|
73 |
demo = gr.ChatInterface(
|
74 |
+
fn=respond,
|
75 |
additional_inputs=[
|
76 |
+
gr.Textbox(
|
77 |
+
value="You are a friendly and helpful Chatbot.",
|
78 |
+
label="System Message",
|
79 |
+
placeholder="Enter the system prompt here...",
|
80 |
+
),
|
81 |
+
gr.Slider(
|
82 |
+
minimum=1,
|
83 |
+
maximum=2048,
|
84 |
+
value=512,
|
85 |
+
step=1,
|
86 |
+
label="Max New Tokens",
|
87 |
+
info="Controls the maximum length of the generated response.",
|
88 |
+
),
|
89 |
+
gr.Slider(
|
90 |
+
minimum=0.1,
|
91 |
+
maximum=4.0,
|
92 |
+
value=0.7,
|
93 |
+
step=0.1,
|
94 |
+
label="Temperature",
|
95 |
+
info="Controls randomness (higher = more creative, lower = more deterministic).",
|
96 |
+
),
|
97 |
gr.Slider(
|
98 |
minimum=0.1,
|
99 |
maximum=1.0,
|
100 |
value=0.95,
|
101 |
step=0.05,
|
102 |
+
label="Top-p (Nucleus Sampling)",
|
103 |
+
info="Controls diversity via nucleus sampling.",
|
104 |
+
),
|
105 |
+
gr.Dropdown(
|
106 |
+
choices=AVAILABLE_MODELS,
|
107 |
+
value=AVAILABLE_MODELS[0],
|
108 |
+
label="Model Selection",
|
109 |
+
info="Select the model to use for inference.",
|
110 |
+
),
|
111 |
+
gr.Textbox(
|
112 |
+
value=HF_TOKEN,
|
113 |
+
label="Hugging Face API Token",
|
114 |
+
type="password",
|
115 |
+
placeholder="Enter your HF API token (or set HF_TOKEN env variable)",
|
116 |
),
|
|
|
|
|
117 |
],
|
118 |
+
title="Chatbot with Hugging Face Inference API",
|
119 |
+
description="Interact with a chatbot powered by Hugging Face models. Provide your API token and customize settings.",
|
120 |
+
theme="soft",
|
121 |
+
submit_btn="Send Message",
|
122 |
+
retry_btn="Retry",
|
123 |
+
undo_btn="Undo",
|
124 |
+
clear_btn="Clear Chat",
|
125 |
)
|
126 |
|
|
|
127 |
if __name__ == "__main__":
|
128 |
+
demo.launch()
|