Spaces:
Running
Running
File size: 6,424 Bytes
71516c7 268d349 71516c7 268d349 d12971a 71516c7 d12971a 71516c7 d12971a 71516c7 d12971a 268d349 d12971a 71516c7 d12971a 268d349 71516c7 4d14c90 e979e74 71516c7 03a5c5e 4d14c90 71516c7 d12971a 71516c7 d12971a 71516c7 d12971a 03a5c5e d12971a 71516c7 03a5c5e 71516c7 d12971a 71516c7 268d349 71516c7 d12971a 268d349 d12971a 71516c7 d12971a 71516c7 d12971a 71516c7 268d349 d12971a |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
import time
# Initialize the client
client = InferenceClient("HuggingFaceH4/starchat2-15b-v0.1")
def respond(
message,
chat_history,
system_message,
max_tokens,
temperature,
top_p,
model_name
):
"""
Generate chat responses using the specified model.
"""
# Update client if model changes
global client
client = InferenceClient(model_name)
messages = [{"role": "system", "content": system_message}]
# Build conversation history
for human_msg, assistant_msg in chat_history:
messages.append({"role": "user", "content": human_msg})
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
response = ""
try:
# Add user message to history immediately
chat_history = chat_history + [(message, None)]
yield chat_history
for token_data in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = token_data.choices[0].delta.content
response += token
# Update the last assistant message
chat_history[-1] = (message, response)
yield chat_history
except Exception as e:
error_msg = f"Error: {str(e)}"
chat_history[-1] = (message, error_msg)
yield chat_history
def create_chat_interface():
"""
Create and configure the Gradio interface
"""
# Default system message
default_system = '''
You are a pragmatic coding assistant specializing in Python. Your task is to strictly respond with **Python code only**, ensuring all explanations and comments are embedded within the script using **multi-line comment blocks** (`### or #`).
**Response Requirements:**
- **No external ### Explanation ### ** All descriptions, justifications, and context must be inside the script.
- **Follow OOP principles** where applicable, improving maintainability and extensibility.
- **Ensure compliance with PEP8 and autopep8 formatting.**
- **Enhance and refactor the provided script**, making it a more efficient, readable, and reusable # IMPROVED PYTHON CODE #.
- **At the end of every script, include a '### Future Features ###' comment block** outlining possible enhancements.
**Example Response Format:**
```python
# filename.py
# Module: Improved Script v1.0
# Description: [Brief explanation of script functionality]
# IMPROVED PYTHON CODE #
### Explanation ###
#- inside comment block.
### Future Features ###
#- Suggested improvement 1
#- Suggested improvement 2
```
Now, improve and enhance the following script:
'''
qwen_options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B", ]
# Available models
models = [
"Qwen/Qwen2.5-Coder-3B-Instruct",
"Qwen/Qwen2.5-Coder-1.5B-Instruct",
"HuggingFaceH4/zephyr-7b-beta",
"HuggingFaceH4/zephyr-7b-alpha",
"HuggingFaceH4/starchat2-15b-v0.1",
"meta-llama/Llama-2-70b-chat-hf",
"mistralai/Mixtral-8x7B-Instruct-v0.1"
]
# Create the interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 Advanced AI Chatbot")
chatbot = gr.Chatbot(
height=600,
show_label=False,
container=True,
)
with gr.Row():
with gr.Column(scale=4):
msg = gr.Textbox(
show_label=False,
placeholder="Type your message here...",
container=False
)
with gr.Column(scale=1, min_width=100):
send = gr.Button("Send")
with gr.Accordion("Settings", open=False):
system_msg = gr.Textbox(
label="System Message",
value=default_system,
lines=20
)
model = gr.Dropdown(
choices=models,
value=models[0],
label="Model"
)
with gr.Row():
with gr.Column():
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature"
)
max_tokens = gr.Slider(
minimum=50,
maximum=4096,
value=2048,
step=1,
label="Max Tokens"
)
with gr.Column():
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.1,
label="Top P"
)
clear = gr.Button("Clear Chat")
# Handle sending messages
msg.submit(
respond,
[msg, chatbot, system_msg, max_tokens, temperature, top_p, model],
[chatbot]
).then(
lambda: "",
None,
msg,
queue=False
)
send.click(
respond,
[msg, chatbot, system_msg, max_tokens, temperature, top_p, model],
[chatbot]
).then(
lambda: "",
None,
msg,
queue=False
)
# Clear chat history
clear.click(lambda: None, None, chatbot, queue=False)
# Example prompts
gr.Examples(
examples=[
["Tell me a short story about a robot learning to paint."],
["Explain quantum computing in simple terms."],
["Write a haiku about artificial intelligence."]
],
inputs=msg
)
return demo
# Create and launch the interface
if __name__ == "__main__":
demo = create_chat_interface()
demo.queue()
demo.launch(
share=False, # Disable sharing on Spaces
) |