File size: 1,913 Bytes
cf38aa5
43b5bef
 
 
cf38aa5
 
43b5bef
 
 
 
 
 
 
 
 
cf38aa5
 
 
 
 
43b5bef
 
cf38aa5
43b5bef
cf38aa5
43b5bef
 
cf38aa5
43b5bef
 
 
 
 
 
 
 
 
 
 
 
 
cf38aa5
43b5bef
 
 
 
 
 
 
 
cf38aa5
43b5bef
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import gradio as gr
import openai

# We'll read the Hugging Face API key from environment variables (using Spaces "secrets").
HF_API_KEY = os.getenv("HF_API_KEY")

# Model endpoints on Hugging Face
MODEL_ENDPOINTS = {
    "Qwen2.5-72B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
    "Llama3.3-70B-Instruct": "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct",
    "Qwen2.5-Coder-32B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B-Instruct",
}

def query_model(prompt, model_endpoint):
    """
    Query a specific model using OpenAI-compatible ChatCompletion.
    Since the Hugging Face Inference API is OpenAI-compatible here,
    we just set openai.api_base to the model's endpoint.
    """
    openai.api_key = HF_API_KEY
    openai.api_base = model_endpoint
    
    response = openai.ChatCompletion.create(
        model="placeholder-model",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=512,
        temperature=0.7,
    )
    return response.choices[0].message["content"]

def chat_with_models(user_input, history):
    responses = []
    for model_name, endpoint in MODEL_ENDPOINTS.items():
        model_response = query_model(user_input, endpoint)
        responses.append(f"**{model_name}**: {model_response}")
    combined_answer = "\n\n".join(responses)
    history.append((user_input, combined_answer))
    return history, history

with gr.Blocks() as demo:
    gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API (OpenAI-compatible)")

    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Your Message")
    clear = gr.Button("Clear")

    def clear_chat():
        return [], []

    msg.submit(fn=chat_with_models, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
    clear.click(fn=clear_chat, outputs=[chatbot, chatbot])

demo.launch()