Update app.py
Browse files
app.py
CHANGED
|
@@ -58,6 +58,104 @@
|
|
| 58 |
# demo.launch()
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
import os
|
| 62 |
import gradio as gr
|
| 63 |
from dotenv import load_dotenv
|
|
@@ -71,9 +169,10 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 71 |
if not HF_TOKEN:
|
| 72 |
raise ValueError("HF_TOKEN is missing.")
|
| 73 |
|
| 74 |
-
# Initialize client
|
| 75 |
client = InferenceClient(
|
| 76 |
-
api_key=HF_TOKEN
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
# System prompt
|
|
@@ -86,59 +185,34 @@ system_message = (
|
|
| 86 |
|
| 87 |
def chat_function(message, history):
|
| 88 |
|
| 89 |
-
messages = [
|
| 90 |
-
{
|
| 91 |
-
"role": "system",
|
| 92 |
-
"content": system_message
|
| 93 |
-
}
|
| 94 |
-
]
|
| 95 |
|
| 96 |
history = history or []
|
| 97 |
|
| 98 |
for item in history:
|
| 99 |
-
|
| 100 |
if isinstance(item, dict):
|
| 101 |
-
|
| 102 |
role = item.get("role")
|
| 103 |
content = item.get("content", "")
|
| 104 |
-
|
| 105 |
if role in ["user", "assistant"]:
|
| 106 |
-
messages.append({
|
| 107 |
-
"role": role,
|
| 108 |
-
"content": content
|
| 109 |
-
})
|
| 110 |
|
| 111 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 112 |
-
|
| 113 |
user_msg, assistant_msg = item
|
| 114 |
-
|
| 115 |
if user_msg:
|
| 116 |
-
messages.append({
|
| 117 |
-
"role": "user",
|
| 118 |
-
"content": user_msg
|
| 119 |
-
})
|
| 120 |
-
|
| 121 |
if assistant_msg:
|
| 122 |
-
messages.append({
|
| 123 |
-
"role": "assistant",
|
| 124 |
-
"content": assistant_msg
|
| 125 |
-
})
|
| 126 |
|
| 127 |
-
messages.append({
|
| 128 |
-
"role": "user",
|
| 129 |
-
"content": message
|
| 130 |
-
})
|
| 131 |
|
| 132 |
try:
|
| 133 |
-
|
| 134 |
completion = client.chat.completions.create(
|
| 135 |
-
model="Qwen/Qwen2.5-Coder-7B-Instruct:nscale"
|
| 136 |
messages=messages,
|
| 137 |
max_tokens=2048,
|
| 138 |
temperature=0.7,
|
| 139 |
top_p=0.95,
|
| 140 |
)
|
| 141 |
-
|
| 142 |
return completion.choices[0].message.content
|
| 143 |
|
| 144 |
except Exception as e:
|
|
@@ -152,6 +226,5 @@ demo = gr.ChatInterface(
|
|
| 152 |
description="A coding assistant powered by Qwen2.5-Coder."
|
| 153 |
)
|
| 154 |
|
| 155 |
-
# Launch
|
| 156 |
if __name__ == "__main__":
|
| 157 |
demo.launch()
|
|
|
|
| 58 |
# demo.launch()
|
| 59 |
|
| 60 |
|
| 61 |
+
# import os
|
| 62 |
+
# import gradio as gr
|
| 63 |
+
# from dotenv import load_dotenv
|
| 64 |
+
# from huggingface_hub import InferenceClient
|
| 65 |
+
|
| 66 |
+
# # Load environment variables
|
| 67 |
+
# load_dotenv()
|
| 68 |
+
|
| 69 |
+
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 70 |
+
|
| 71 |
+
# if not HF_TOKEN:
|
| 72 |
+
# raise ValueError("HF_TOKEN is missing.")
|
| 73 |
+
|
| 74 |
+
# # Initialize client
|
| 75 |
+
# client = InferenceClient(
|
| 76 |
+
# api_key=HF_TOKEN
|
| 77 |
+
# )
|
| 78 |
+
|
| 79 |
+
# # System prompt
|
| 80 |
+
# system_message = (
|
| 81 |
+
# "You are a helpful and experienced coding assistant specialized in web development. "
|
| 82 |
+
# "Help the user by generating complete and functional code for building websites. "
|
| 83 |
+
# "You can provide HTML, CSS, JavaScript, and backend code like Flask, Node.js, etc. "
|
| 84 |
+
# "based on their requirements."
|
| 85 |
+
# )
|
| 86 |
+
|
| 87 |
+
# def chat_function(message, history):
|
| 88 |
+
|
| 89 |
+
# messages = [
|
| 90 |
+
# {
|
| 91 |
+
# "role": "system",
|
| 92 |
+
# "content": system_message
|
| 93 |
+
# }
|
| 94 |
+
# ]
|
| 95 |
+
|
| 96 |
+
# history = history or []
|
| 97 |
+
|
| 98 |
+
# for item in history:
|
| 99 |
+
|
| 100 |
+
# if isinstance(item, dict):
|
| 101 |
+
|
| 102 |
+
# role = item.get("role")
|
| 103 |
+
# content = item.get("content", "")
|
| 104 |
+
|
| 105 |
+
# if role in ["user", "assistant"]:
|
| 106 |
+
# messages.append({
|
| 107 |
+
# "role": role,
|
| 108 |
+
# "content": content
|
| 109 |
+
# })
|
| 110 |
+
|
| 111 |
+
# elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 112 |
+
|
| 113 |
+
# user_msg, assistant_msg = item
|
| 114 |
+
|
| 115 |
+
# if user_msg:
|
| 116 |
+
# messages.append({
|
| 117 |
+
# "role": "user",
|
| 118 |
+
# "content": user_msg
|
| 119 |
+
# })
|
| 120 |
+
|
| 121 |
+
# if assistant_msg:
|
| 122 |
+
# messages.append({
|
| 123 |
+
# "role": "assistant",
|
| 124 |
+
# "content": assistant_msg
|
| 125 |
+
# })
|
| 126 |
+
|
| 127 |
+
# messages.append({
|
| 128 |
+
# "role": "user",
|
| 129 |
+
# "content": message
|
| 130 |
+
# })
|
| 131 |
+
|
| 132 |
+
# try:
|
| 133 |
+
|
| 134 |
+
# completion = client.chat.completions.create(
|
| 135 |
+
# model="Qwen/Qwen2.5-Coder-7B-Instruct:nscale",
|
| 136 |
+
# messages=messages,
|
| 137 |
+
# max_tokens=2048,
|
| 138 |
+
# temperature=0.7,
|
| 139 |
+
# top_p=0.95,
|
| 140 |
+
# )
|
| 141 |
+
|
| 142 |
+
# return completion.choices[0].message.content
|
| 143 |
+
|
| 144 |
+
# except Exception as e:
|
| 145 |
+
# return f"Error: {str(e)}"
|
| 146 |
+
|
| 147 |
+
# # Interface
|
| 148 |
+
# demo = gr.ChatInterface(
|
| 149 |
+
# fn=chat_function,
|
| 150 |
+
# type="messages",
|
| 151 |
+
# title="AI Coding Assistant",
|
| 152 |
+
# description="A coding assistant powered by Qwen2.5-Coder."
|
| 153 |
+
# )
|
| 154 |
+
|
| 155 |
+
# # Launch
|
| 156 |
+
# if __name__ == "__main__":
|
| 157 |
+
# demo.launch()
|
| 158 |
+
|
| 159 |
import os
|
| 160 |
import gradio as gr
|
| 161 |
from dotenv import load_dotenv
|
|
|
|
| 169 |
if not HF_TOKEN:
|
| 170 |
raise ValueError("HF_TOKEN is missing.")
|
| 171 |
|
| 172 |
+
# Initialize client with provider specified
|
| 173 |
client = InferenceClient(
|
| 174 |
+
api_key=HF_TOKEN,
|
| 175 |
+
provider="nscale", # <-- explicitly set the provider here
|
| 176 |
)
|
| 177 |
|
| 178 |
# System prompt
|
|
|
|
| 185 |
|
| 186 |
def chat_function(message, history):
|
| 187 |
|
| 188 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
history = history or []
|
| 191 |
|
| 192 |
for item in history:
|
|
|
|
| 193 |
if isinstance(item, dict):
|
|
|
|
| 194 |
role = item.get("role")
|
| 195 |
content = item.get("content", "")
|
|
|
|
| 196 |
if role in ["user", "assistant"]:
|
| 197 |
+
messages.append({"role": role, "content": content})
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
|
|
|
| 200 |
user_msg, assistant_msg = item
|
|
|
|
| 201 |
if user_msg:
|
| 202 |
+
messages.append({"role": "user", "content": user_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
if assistant_msg:
|
| 204 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
try:
|
|
|
|
| 209 |
completion = client.chat.completions.create(
|
| 210 |
+
model="Qwen/Qwen2.5-Coder-7B-Instruct", # <-- remove ":nscale" suffix
|
| 211 |
messages=messages,
|
| 212 |
max_tokens=2048,
|
| 213 |
temperature=0.7,
|
| 214 |
top_p=0.95,
|
| 215 |
)
|
|
|
|
| 216 |
return completion.choices[0].message.content
|
| 217 |
|
| 218 |
except Exception as e:
|
|
|
|
| 226 |
description="A coding assistant powered by Qwen2.5-Coder."
|
| 227 |
)
|
| 228 |
|
|
|
|
| 229 |
if __name__ == "__main__":
|
| 230 |
demo.launch()
|