Spaces:
Sleeping
Sleeping
File size: 11,461 Bytes
4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 d542847 4274672 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# import gradio as gr
# from huggingface_hub import InferenceClient
# """
# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
# """
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# def respond(
# message,
# history: list[tuple[str, str]],
# system_message,
# max_tokens,
# temperature,
# top_p,
# ):
# messages = [{"role": "system", "content": system_message}]
# for val in history:
# if val[0]:
# messages.append({"role": "user", "content": val[0]})
# if val[1]:
# messages.append({"role": "assistant", "content": val[1]})
# messages.append({"role": "user", "content": message})
# response = ""
# for message in client.chat_completion(
# messages,
# max_tokens=max_tokens,
# stream=True,
# temperature=temperature,
# top_p=top_p,
# ):
# token = message.choices[0].delta.content
# response += token
# yield response
# """
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
# """
# demo = gr.ChatInterface(
# respond,
# additional_inputs=[
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
# gr.Slider(
# minimum=0.1,
# maximum=1.0,
# value=0.95,
# step=0.05,
# label="Top-p (nucleus sampling)",
# ),
# ],
# )
# if __name__ == "__main__":
# demo.launch()
import gradio as gr
from huggingface_hub import InferenceClient
import time
import random
from datetime import datetime
# Theme and styling constants
THEME = gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
radius_size=gr.themes.sizes.radius_sm,
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
)
# Configuration
MODEL_ID = "HuggingFaceH4/zephyr-7b-beta"
DEFAULT_SYSTEM_MSG = "You are a helpful, friendly, and knowledgeable AI assistant."
# Initialize the client
client = InferenceClient(MODEL_ID)
def format_history(history):
"""Helper function to format chat history for display"""
formatted = []
for user_msg, ai_msg in history:
if user_msg:
formatted.append({"role": "user", "content": user_msg})
if ai_msg:
formatted.append({"role": "assistant", "content": ai_msg})
return formatted
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
model_id,
typing_animation=True
):
"""Generate response from the model with typing animation effect"""
# Format messages for the API
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
# Use the selected model
inference_client = InferenceClient(model_id)
# Generate response with typing animation
response = ""
for message in inference_client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
if token:
response += token
# If typing animation is enabled, add a small random delay
if typing_animation:
time.sleep(random.uniform(0.01, 0.03))
yield response
def create_interface():
"""Create and configure the Gradio interface"""
# Available models dropdown
models = [
"HuggingFaceH4/zephyr-7b-beta",
"mistralai/Mistral-7B-Instruct-v0.2",
"meta-llama/Llama-2-7b-chat-hf",
"gpt2" # Fallback for quick testing
]
# Custom CSS for better styling
css = """
.gradio-container {
min-height: 100vh;
}
.message-bubble {
padding: 10px 15px;
border-radius: 12px;
margin-bottom: 8px;
}
.user-bubble {
background-color: #e9f5ff;
margin-left: 20px;
}
.bot-bubble {
background-color: #f0f4f9;
margin-right: 20px;
}
.timestamp {
font-size: 0.7em;
color: #888;
margin-top: 2px;
}
"""
with gr.Blocks(theme=THEME, css=css) as demo:
gr.Markdown("# 🤖 Enhanced AI Chat Interface")
gr.Markdown("Chat with state-of-the-art language models from Hugging Face")
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
label="Conversation",
bubble_full_width=False,
height=600,
avatar_images=("👤", "🤖"),
show_copy_button=True
)
with gr.Row():
msg = gr.Textbox(
placeholder="Type your message here...",
show_label=False,
container=False,
scale=9
)
submit_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Accordion("Conversation Summary", open=False):
summary = gr.Textbox(label="Key points from this conversation", lines=3, interactive=False)
summary_btn = gr.Button("Generate Summary", variant="secondary")
with gr.Column(scale=1):
with gr.Accordion("Model Settings", open=True):
model_selection = gr.Dropdown(
models,
value=MODEL_ID,
label="Select Model",
info="Choose which AI model to chat with"
)
system_msg = gr.Textbox(
value=DEFAULT_SYSTEM_MSG,
label="System Message",
info="Instructions that define how the AI behaves",
lines=3
)
max_tokens = gr.Slider(
minimum=1,
maximum=2048,
value=512,
step=1,
label="Max New Tokens",
info="Maximum length of generated response"
)
with gr.Row():
with gr.Column():
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature",
info="Higher = more creative, Lower = more focused"
)
with gr.Column():
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p",
info="Controls randomness in token selection"
)
typing_effect = gr.Checkbox(
label="Enable Typing Animation",
value=True,
info="Show realistic typing animation"
)
with gr.Accordion("Tools", open=False):
clear_btn = gr.Button("Clear Conversation", variant="secondary")
export_btn = gr.Button("Export Chat History", variant="secondary")
chat_download = gr.File(label="Download", interactive=False, visible=False)
# Event handlers
msg_submit = msg.submit(
fn=respond,
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p, model_selection, typing_effect],
outputs=[chatbot],
queue=True
)
submit_click = submit_btn.click(
fn=respond,
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p, model_selection, typing_effect],
outputs=[chatbot],
queue=True
)
# Clear the input field after sending
msg_submit.then(lambda: "", None, msg)
submit_click.then(lambda: "", None, msg)
# Clear chat history
def clear_history():
return None
clear_btn.click(
fn=clear_history,
inputs=[],
outputs=[chatbot]
)
# Export chat history
def export_history(history):
if not history:
return None
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"chat_history_{timestamp}.txt"
with open(filename, "w") as f:
f.write("# Chat History\n\n")
f.write(f"Exported on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
for user_msg, ai_msg in history:
f.write(f"## User\n{user_msg}\n\n")
f.write(f"## AI\n{ai_msg}\n\n")
f.write("---\n\n")
return filename
export_btn.click(
fn=export_history,
inputs=[chatbot],
outputs=[chat_download],
queue=False
).then(
lambda: gr.update(visible=True),
None,
[chat_download]
)
# Generate conversation summary (simplified implementation)
def generate_summary(history):
if not history or len(history) < 2:
return "Not enough conversation to summarize yet."
# In a real application, you might want to send this to the model
# Here we're just creating a simple summary
topics = []
for user_msg, _ in history:
if user_msg and len(user_msg.split()) > 3: # Simple heuristic
topics.append(user_msg.split()[0:3])
if topics:
return f"This conversation covered {len(history)} exchanges about various topics."
else:
return "Brief conversation with no clear topics."
summary_btn.click(
fn=generate_summary,
inputs=[chatbot],
outputs=[summary]
)
return demo
# Create and launch the interface
demo = create_interface()
if __name__ == "__main__":
demo.launch(share=False, debug=False)
|