File size: 1,832 Bytes
b8b5a9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
import gradio as gr
from huggingface_hub import AsyncInferenceClient
from openai import AsyncOpenAI


async def hf_chat(message, history, api_key: str = "") -> str:
    """
    使用 Hugging Face 的聊天模型進行對話。
    """
    if not api_key:
        raise gr.Error("API key is required for Hugging Face Inference API.")

    hf_client = AsyncInferenceClient(
        provider="fireworks-ai",
        api_key=api_key,
    )
    response = await hf_client.chat_completion(
        model="Qwen/Qwen3-30B-A3B",
        messages=history + [{"role": "user", "content": message}],
    )  # type: ignore
    return response.choices[0].message.content


async def oai_chat(message, history, api_key: str = "") -> str:
    if not api_key:
        raise gr.Error("API key is required for OpenAI API.")

    oai_client = AsyncOpenAI(api_key=api_key)
    response = await oai_client.chat.completions.create(
        model="gpt-4.1-nano",
        messages=history + [{"role": "user", "content": message}],
    )  # type: ignore
    return response.choices[0].message.content


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            gr.Image("img/llama_with_hats.png")
    with gr.Row():
        with gr.Column():
            chat = gr.ChatInterface(
                fn=hf_chat,  # 使用 Hugging Face 的聊天模型
                title="Chat with LLM",  # 標題
                type="messages",
                additional_inputs=[
                    gr.Textbox(
                        label="Hugging Face API Key",
                        placeholder="Enter your Hugging Face API key here",
                        type="password",
                        value="",
                    ),
                ],
            )

demo.launch(share=True, debug=True)  # 啟動 Gradio 介面,並分享連