File size: 1,044 Bytes
a17d693
 
7adc148
a17d693
7adc148
a17d693
7adc148
 
0b378fc
7adc148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a17d693
7adc148
96cad13
7adc148
 
 
 
 
 
 
 
 
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
import os
from dotenv import load_dotenv
import gradio as gr
from huggingface_hub import InferenceClient

load_dotenv()

HF_Token = os.getenv("HUGGINGFACEHUB_ACCESS_TOKEN")
MODEL_ID = "MiniMaxAI/MiniMax-M2.5"

client = InferenceClient(
    model=MODEL_ID,
    token=HF_Token
)

messages = [
    {"role": "system", "content": "Hello, my name is John. What is your name?"}
]

def chat(user_text):
    if not user_text:
        return ""

    messages.append({"role": "user", "content": user_text})

    try:
        resp = client.chat_completion(
            messages=messages,
            max_tokens=150,
            temperature=0.7
        )
        bot_text = resp.choices[0].message["content"]
        messages.append({"role": "assistant", "content": bot_text})
        return bot_text
    except Exception as e:
        messages.pop()
        return f"Error: {str(e)}"

demo = gr.Interface(
    fn=chat,
    inputs=gr.Textbox(label="You"),
    outputs=gr.Textbox(label="Bot"),
    title="Gemma Chatbot (Hugging Face Space)"
)

demo.launch()