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