Update app.py
Browse files
app.py
CHANGED
|
@@ -113,53 +113,100 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
| 113 |
# if __name__ == "__main__":
|
| 114 |
# demo.launch()
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
import gradio as gr
|
| 117 |
from huggingface_hub import InferenceClient
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
"""
|
| 122 |
-
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 123 |
|
| 124 |
def respond(message, history: list[tuple[str, str]]):
|
| 125 |
system_message = (
|
| 126 |
"You are a helpful and experienced coding assistant specialized in web development. "
|
| 127 |
"Help the user by generating complete and functional code for building websites. "
|
| 128 |
-
"You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.)
|
| 129 |
-
"
|
| 130 |
)
|
| 131 |
max_tokens = 2048
|
| 132 |
temperature = 0.7
|
| 133 |
top_p = 0.95
|
| 134 |
|
|
|
|
| 135 |
messages = [{"role": "system", "content": system_message}]
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 142 |
-
|
| 143 |
messages.append({"role": "user", "content": message})
|
| 144 |
|
| 145 |
response = ""
|
| 146 |
-
|
| 147 |
-
for
|
| 148 |
-
|
|
|
|
| 149 |
max_tokens=max_tokens,
|
| 150 |
stream=True,
|
| 151 |
temperature=temperature,
|
| 152 |
top_p=top_p,
|
| 153 |
):
|
| 154 |
-
token
|
| 155 |
-
|
| 156 |
response += token
|
| 157 |
yield response
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
"""
|
| 162 |
-
demo = gr.ChatInterface(respond)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
| 165 |
demo.launch()
|
|
@@ -167,3 +214,4 @@ if __name__ == "__main__":
|
|
| 167 |
|
| 168 |
|
| 169 |
|
|
|
|
|
|
| 113 |
# if __name__ == "__main__":
|
| 114 |
# demo.launch()
|
| 115 |
|
| 116 |
+
# import gradio as gr
|
| 117 |
+
# from huggingface_hub import InferenceClient
|
| 118 |
+
|
| 119 |
+
# """
|
| 120 |
+
# 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
|
| 121 |
+
# """
|
| 122 |
+
# client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 123 |
+
|
| 124 |
+
# def respond(message, history: list[tuple[str, str]]):
|
| 125 |
+
# system_message = (
|
| 126 |
+
# "You are a helpful and experienced coding assistant specialized in web development. "
|
| 127 |
+
# "Help the user by generating complete and functional code for building websites. "
|
| 128 |
+
# "You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) based on their requirements. "
|
| 129 |
+
# "Break down the tasks clearly if needed, and be friendly and supportive in your responses."
|
| 130 |
+
# )
|
| 131 |
+
# max_tokens = 2048
|
| 132 |
+
# temperature = 0.7
|
| 133 |
+
# top_p = 0.95
|
| 134 |
+
|
| 135 |
+
# messages = [{"role": "system", "content": system_message}]
|
| 136 |
+
|
| 137 |
+
# for val in history:
|
| 138 |
+
# if val[0]:
|
| 139 |
+
# messages.append({"role": "user", "content": val[0]})
|
| 140 |
+
# if val[1]:
|
| 141 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
| 142 |
+
|
| 143 |
+
# messages.append({"role": "user", "content": message})
|
| 144 |
+
|
| 145 |
+
# response = ""
|
| 146 |
+
|
| 147 |
+
# for message in client.chat_completion(
|
| 148 |
+
# messages,
|
| 149 |
+
# max_tokens=max_tokens,
|
| 150 |
+
# stream=True,
|
| 151 |
+
# temperature=temperature,
|
| 152 |
+
# top_p=top_p,
|
| 153 |
+
# ):
|
| 154 |
+
# token = message.choices[0].delta.content
|
| 155 |
+
|
| 156 |
+
# response += token
|
| 157 |
+
# yield response
|
| 158 |
+
|
| 159 |
+
# """
|
| 160 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 161 |
+
# """
|
| 162 |
+
# demo = gr.ChatInterface(respond)
|
| 163 |
+
|
| 164 |
+
# if __name__ == "__main__":
|
| 165 |
+
# demo.launch()
|
| 166 |
+
|
| 167 |
import gradio as gr
|
| 168 |
from huggingface_hub import InferenceClient
|
| 169 |
|
| 170 |
+
# 1. Instantiate with named model param
|
| 171 |
+
client = InferenceClient(model="Qwen/Qwen2.5-Coder-32B-Instruct")
|
|
|
|
|
|
|
| 172 |
|
| 173 |
def respond(message, history: list[tuple[str, str]]):
|
| 174 |
system_message = (
|
| 175 |
"You are a helpful and experienced coding assistant specialized in web development. "
|
| 176 |
"Help the user by generating complete and functional code for building websites. "
|
| 177 |
+
"You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
|
| 178 |
+
"based on their requirements."
|
| 179 |
)
|
| 180 |
max_tokens = 2048
|
| 181 |
temperature = 0.7
|
| 182 |
top_p = 0.95
|
| 183 |
|
| 184 |
+
# Build messages in OpenAI-compatible format
|
| 185 |
messages = [{"role": "system", "content": system_message}]
|
| 186 |
+
for user_msg, assistant_msg in history:
|
| 187 |
+
if user_msg:
|
| 188 |
+
messages.append({"role": "user", "content": user_msg})
|
| 189 |
+
if assistant_msg:
|
| 190 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 191 |
messages.append({"role": "user", "content": message})
|
| 192 |
|
| 193 |
response = ""
|
| 194 |
+
# 2. Use named parameters and alias if desired
|
| 195 |
+
for chunk in client.chat.completions.create(
|
| 196 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 197 |
+
messages=messages,
|
| 198 |
max_tokens=max_tokens,
|
| 199 |
stream=True,
|
| 200 |
temperature=temperature,
|
| 201 |
top_p=top_p,
|
| 202 |
):
|
| 203 |
+
# 3. Extract token content
|
| 204 |
+
token = chunk.choices[0].delta.content or ""
|
| 205 |
response += token
|
| 206 |
yield response
|
| 207 |
|
| 208 |
+
# 4. Wire up Gradio chat interface
|
| 209 |
+
demo = gr.ChatInterface(respond, type="messages")
|
|
|
|
|
|
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
demo.launch()
|
|
|
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
+
|