Update app.py
Browse files
app.py
CHANGED
@@ -309,6 +309,63 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
309 |
|
310 |
# demo = gr.ChatInterface(respond, type="messages")
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
# if __name__ == "__main__":
|
313 |
# demo.launch()
|
314 |
import os
|
@@ -316,41 +373,43 @@ import gradio as gr
|
|
316 |
from huggingface_hub import InferenceClient
|
317 |
from dotenv import load_dotenv
|
318 |
|
319 |
-
# Load
|
320 |
load_dotenv()
|
321 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
322 |
|
323 |
-
# Initialize
|
324 |
client = InferenceClient(
|
325 |
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
326 |
token=HF_TOKEN
|
327 |
)
|
328 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
def respond(message, history):
|
330 |
-
"""
|
331 |
-
Chat response generator function streaming from Hugging Face Inference API.
|
332 |
-
"""
|
333 |
-
system_message = (
|
334 |
-
"You are a helpful and experienced coding assistant specialized in web development. "
|
335 |
-
"Help the user by generating complete and functional code for building websites. "
|
336 |
-
"You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
|
337 |
-
"based on their requirements."
|
338 |
-
)
|
339 |
max_tokens = 2048
|
340 |
temperature = 0.7
|
341 |
top_p = 0.95
|
342 |
|
343 |
-
#
|
344 |
messages = [{"role": "system", "content": system_message}]
|
345 |
-
for
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
|
|
350 |
messages.append({"role": "user", "content": message})
|
351 |
|
352 |
response = ""
|
353 |
-
|
|
|
354 |
for chunk in client.chat.completions.create(
|
355 |
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
356 |
messages=messages,
|
@@ -359,12 +418,18 @@ def respond(message, history):
|
|
359 |
temperature=temperature,
|
360 |
top_p=top_p,
|
361 |
):
|
362 |
-
token = chunk.choices[0].delta.get("content"
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
368 |
|
369 |
if __name__ == "__main__":
|
370 |
demo.launch()
|
@@ -376,3 +441,4 @@ if __name__ == "__main__":
|
|
376 |
|
377 |
|
378 |
|
|
|
|
309 |
|
310 |
# demo = gr.ChatInterface(respond, type="messages")
|
311 |
|
312 |
+
# if __name__ == "__main__":
|
313 |
+
# demo.launch()
|
314 |
+
# import os
|
315 |
+
# import gradio as gr
|
316 |
+
# from huggingface_hub import InferenceClient
|
317 |
+
# from dotenv import load_dotenv
|
318 |
+
|
319 |
+
# # Load .env variables (make sure to have HF_TOKEN in .env or set as env var)
|
320 |
+
# load_dotenv()
|
321 |
+
# HF_TOKEN = os.getenv("HF_TOKEN") # or directly assign your token here as string
|
322 |
+
|
323 |
+
# # Initialize InferenceClient with Hugging Face API token
|
324 |
+
# client = InferenceClient(
|
325 |
+
# model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
326 |
+
# token=HF_TOKEN
|
327 |
+
# )
|
328 |
+
|
329 |
+
# def respond(message, history):
|
330 |
+
# """
|
331 |
+
# Chat response generator function streaming from Hugging Face Inference API.
|
332 |
+
# """
|
333 |
+
# system_message = (
|
334 |
+
# "You are a helpful and experienced coding assistant specialized in web development. "
|
335 |
+
# "Help the user by generating complete and functional code for building websites. "
|
336 |
+
# "You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
|
337 |
+
# "based on their requirements."
|
338 |
+
# )
|
339 |
+
# max_tokens = 2048
|
340 |
+
# temperature = 0.7
|
341 |
+
# top_p = 0.95
|
342 |
+
|
343 |
+
# # Prepare messages in OpenAI chat format
|
344 |
+
# messages = [{"role": "system", "content": system_message}]
|
345 |
+
# for user_msg, assistant_msg in history:
|
346 |
+
# if user_msg:
|
347 |
+
# messages.append({"role": "user", "content": user_msg})
|
348 |
+
# if assistant_msg:
|
349 |
+
# messages.append({"role": "assistant", "content": assistant_msg})
|
350 |
+
# messages.append({"role": "user", "content": message})
|
351 |
+
|
352 |
+
# response = ""
|
353 |
+
# # Stream response tokens from Hugging Face Inference API
|
354 |
+
# for chunk in client.chat.completions.create(
|
355 |
+
# model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
356 |
+
# messages=messages,
|
357 |
+
# max_tokens=max_tokens,
|
358 |
+
# stream=True,
|
359 |
+
# temperature=temperature,
|
360 |
+
# top_p=top_p,
|
361 |
+
# ):
|
362 |
+
# token = chunk.choices[0].delta.get("content", "")
|
363 |
+
# response += token
|
364 |
+
# yield response
|
365 |
+
|
366 |
+
# # Create Gradio chat interface
|
367 |
+
# demo = gr.ChatInterface(fn=respond, title="Website Building Assistant")
|
368 |
+
|
369 |
# if __name__ == "__main__":
|
370 |
# demo.launch()
|
371 |
import os
|
|
|
373 |
from huggingface_hub import InferenceClient
|
374 |
from dotenv import load_dotenv
|
375 |
|
376 |
+
# Load environment variables
|
377 |
load_dotenv()
|
378 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Ensure this is set in .env
|
379 |
|
380 |
+
# Initialize Hugging Face Inference Client
|
381 |
client = InferenceClient(
|
382 |
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
383 |
token=HF_TOKEN
|
384 |
)
|
385 |
|
386 |
+
# Define system instructions for the chatbot
|
387 |
+
system_message = (
|
388 |
+
"You are a helpful and experienced coding assistant specialized in web development. "
|
389 |
+
"Help the user by generating complete and functional code for building websites. "
|
390 |
+
"You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
|
391 |
+
"based on their requirements."
|
392 |
+
)
|
393 |
+
|
394 |
+
# Define the response generation function
|
395 |
def respond(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
max_tokens = 2048
|
397 |
temperature = 0.7
|
398 |
top_p = 0.95
|
399 |
|
400 |
+
# Convert chat history into OpenAI-style format
|
401 |
messages = [{"role": "system", "content": system_message}]
|
402 |
+
for item in history:
|
403 |
+
role = item["role"]
|
404 |
+
content = item["content"]
|
405 |
+
messages.append({"role": role, "content": content})
|
406 |
+
|
407 |
+
# Add the latest user message
|
408 |
messages.append({"role": "user", "content": message})
|
409 |
|
410 |
response = ""
|
411 |
+
|
412 |
+
# Streaming response from the Hugging Face Inference API
|
413 |
for chunk in client.chat.completions.create(
|
414 |
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
415 |
messages=messages,
|
|
|
418 |
temperature=temperature,
|
419 |
top_p=top_p,
|
420 |
):
|
421 |
+
token = chunk.choices[0].delta.get("content")
|
422 |
+
if token is not None:
|
423 |
+
response += token
|
424 |
+
yield response
|
425 |
+
|
426 |
+
# Create Gradio Chat Interface
|
427 |
+
demo = gr.ChatInterface(
|
428 |
+
fn=respond,
|
429 |
+
title="Website Building Assistant",
|
430 |
+
chatbot=gr.Chatbot(show_label=False),
|
431 |
+
type="openai", # Use OpenAI-style message format
|
432 |
+
)
|
433 |
|
434 |
if __name__ == "__main__":
|
435 |
demo.launch()
|
|
|
441 |
|
442 |
|
443 |
|
444 |
+
|