Spaces:
Running
Running
| 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() | |