File size: 1,020 Bytes
09c402a
bd57a4d
09c402a
a9e5c9d
09c402a
bd57a4d
a9e5c9d
bd57a4d
09c402a
 
bd57a4d
 
09c402a
bd57a4d
 
 
 
6d0b03d
 
bd57a4d
 
 
 
 
 
09c402a
 
 
 
 
bd57a4d
09c402a
 
 
 
bd57a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
from huggingface_hub import InferenceClient

MODEL_ID = "mistralai/Mistral-Small-3.2-24B-Instruct-2506"

# Initialize HF InferenceClient
omodel = InferenceClient(model=MODEL_ID)

# Function to generate response
def chat_with_model(message, history):
    # Build messages in OpenAI-style format
    messages = []
    for user, bot in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})
    messages.append({"role": "user", "content": message})

    # Call Hugging Face Inference API (chat_completion style)
    response = omodel.chat_completion(
        model=MODEL_ID,
        messages=messages,
        max_tokens=512,
    )

    return response.choices[0].message["content"]

# Build Gradio Chat Interface
demo = gr.ChatInterface(
    fn=chat_with_model,
    title="Chat with HuggingFace Mistral",
    description="Gradio + HuggingFace InferenceClient",
    theme="soft"
)

if __name__ == "__main__":
    demo.launch()