MultiModelCoder / app.py
w1r4's picture
Update app.py
332a998 verified
raw
history blame
2.29 kB
import gradio as gr
import os
from huggingface_hub import InferenceClient
# Use Model
model_id = "deepseek-ai/deepseek-coder-33b-instruct"
def respond(message, history, system_message, temperature, request: gr.Request):
# --- 1. Authentication Logic ---
token = None
# Safely access token (handles both Local run and Spaces)
if request:
token = getattr(request, "token", None)
if token is None:
token = os.getenv("HF_TOKEN")
if token is None:
yield "Error: No authentication token found. Please add 'HF_TOKEN' to Space Secrets."
return
# --- 2. Setup Client ---
client = InferenceClient(model_id, token=token)
# --- 3. Build Messages (handling history correctly) ---
messages = [{"role": "system", "content": system_message}]
# We use type="messages" in ChatInterface, so history is already a list of dicts
for msg in history:
messages.append(msg)
messages.append({"role": "user", "content": message})
# --- 4. Generate Response ---
try:
stream = client.chat_completion(
messages,
max_tokens=2048,
stream=True,
temperature=temperature,
top_p=0.9
)
response_text = ""
for chunk in stream:
# FIX: Check if choices exist before accessing index [0]
if not chunk.choices:
continue
content = chunk.choices[0].delta.content
if content:
response_text += content
yield response_text
except Exception as e:
yield f"Error: {str(e)}"
# --- 5. Build UI ---
with gr.Blocks(fill_height=True) as demo:
with gr.Sidebar():
gr.Markdown("# AI Coding Assistant")
gr.Markdown(f"Running **{model_id}**")
gr.LoginButton("Sign in")
gr.ChatInterface(
respond,
# 'type="messages"' fixes the deprecation warning and makes parsing easier
type="messages",
additional_inputs=[
gr.Textbox(value="You are a helpful assistant.", label="System Instruction", lines=2),
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
]
)
demo.launch()