Update app.py
Browse files
app.py
CHANGED
|
@@ -368,6 +368,72 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
| 368 |
|
| 369 |
# if __name__ == "__main__":
|
| 370 |
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
import os
|
| 372 |
import gradio as gr
|
| 373 |
from huggingface_hub import InferenceClient
|
|
@@ -375,15 +441,15 @@ from dotenv import load_dotenv
|
|
| 375 |
|
| 376 |
# Load environment variables
|
| 377 |
load_dotenv()
|
| 378 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 379 |
|
| 380 |
# Initialize Hugging Face Inference Client
|
| 381 |
client = InferenceClient(
|
| 382 |
-
model="
|
| 383 |
token=HF_TOKEN
|
| 384 |
)
|
| 385 |
|
| 386 |
-
#
|
| 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. "
|
|
@@ -391,46 +457,34 @@ system_message = (
|
|
| 391 |
"based on their requirements."
|
| 392 |
)
|
| 393 |
|
| 394 |
-
#
|
| 395 |
def respond(message, history):
|
| 396 |
-
|
| 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
|
| 403 |
-
|
| 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="
|
| 415 |
messages=messages,
|
| 416 |
-
max_tokens=
|
|
|
|
|
|
|
| 417 |
stream=True,
|
| 418 |
-
temperature=temperature,
|
| 419 |
-
top_p=top_p,
|
| 420 |
):
|
| 421 |
-
token = chunk.choices[0].delta.get("content")
|
| 422 |
-
|
| 423 |
-
|
| 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()
|
| 436 |
|
|
@@ -441,4 +495,3 @@ if __name__ == "__main__":
|
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
-
|
|
|
|
| 368 |
|
| 369 |
# if __name__ == "__main__":
|
| 370 |
# demo.launch()
|
| 371 |
+
# import os
|
| 372 |
+
# import gradio as gr
|
| 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,
|
| 416 |
+
# max_tokens=max_tokens,
|
| 417 |
+
# stream=True,
|
| 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()# app.py
|
| 436 |
+
|
| 437 |
import os
|
| 438 |
import gradio as gr
|
| 439 |
from huggingface_hub import InferenceClient
|
|
|
|
| 441 |
|
| 442 |
# Load environment variables
|
| 443 |
load_dotenv()
|
| 444 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 445 |
|
| 446 |
# Initialize Hugging Face Inference Client
|
| 447 |
client = InferenceClient(
|
| 448 |
+
model="mistralai/Devstral-Small-2505",
|
| 449 |
token=HF_TOKEN
|
| 450 |
)
|
| 451 |
|
| 452 |
+
# System prompt for coding assistant
|
| 453 |
system_message = (
|
| 454 |
"You are a helpful and experienced coding assistant specialized in web development. "
|
| 455 |
"Help the user by generating complete and functional code for building websites. "
|
|
|
|
| 457 |
"based on their requirements."
|
| 458 |
)
|
| 459 |
|
| 460 |
+
# Streaming chatbot logic
|
| 461 |
def respond(message, history):
|
| 462 |
+
# Prepare messages with system prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
messages = [{"role": "system", "content": system_message}]
|
| 464 |
+
for msg in history:
|
| 465 |
+
messages.append(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
messages.append({"role": "user", "content": message})
|
| 467 |
|
| 468 |
+
# Stream response from the model
|
| 469 |
response = ""
|
|
|
|
|
|
|
| 470 |
for chunk in client.chat.completions.create(
|
| 471 |
+
model="mistralai/Devstral-Small-2505",
|
| 472 |
messages=messages,
|
| 473 |
+
max_tokens=1024,
|
| 474 |
+
temperature=0.7,
|
| 475 |
+
top_p=0.95,
|
| 476 |
stream=True,
|
|
|
|
|
|
|
| 477 |
):
|
| 478 |
+
token = chunk.choices[0].delta.get("content", "") or ""
|
| 479 |
+
response += token
|
| 480 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
+
# Create Gradio interface
|
| 483 |
+
with gr.Blocks() as demo:
|
| 484 |
+
chatbot = gr.Chatbot(type='messages') # Use modern message format
|
| 485 |
+
gr.ChatInterface(fn=respond, chatbot=chatbot, type="messages") # Match format
|
| 486 |
+
|
| 487 |
+
# Launch app
|
| 488 |
if __name__ == "__main__":
|
| 489 |
demo.launch()
|
| 490 |
|
|
|
|
| 495 |
|
| 496 |
|
| 497 |
|
|
|