Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # ========= SETTINGS ========== | |
| MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3" | |
| BOT_NAME = "XYZ BUSINESS AI" | |
| SYSTEM_PROMPT = f"""You are {BOT_NAME}, a friendly and professional business assistant. | |
| You explain complex ideas clearly, maintain a confident and concise tone, | |
| and help users understand business and finance concepts easily. | |
| """ | |
| # Use HF_TOKEN from secrets (set in Hugging Face Space -> Settings -> Secrets) | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # Initialize client | |
| client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN) | |
| # ========= CHAT LOGIC ========== | |
| def generate_response(user_message, chat_history, max_new_tokens=256, temperature=0.7, top_p=0.9): | |
| try: | |
| # Prepare the full message list | |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| for user, bot in chat_history: | |
| messages.append({"role": "user", "content": user}) | |
| messages.append({"role": "assistant", "content": bot}) | |
| messages.append({"role": "user", "content": user_message}) | |
| # Stream the response | |
| response_text = "" | |
| for chunk in client.chat_completion( | |
| messages=messages, | |
| max_tokens=max_new_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True, | |
| ): | |
| delta = chunk.choices[0].delta.get("content", "") | |
| response_text += delta | |
| yield response_text | |
| except Exception as e: | |
| yield f"⚠️ Error: {str(e)}" | |
| # ========= GRADIO UI ========== | |
| def respond(message, chat_history): | |
| response_stream = generate_response(message, chat_history) | |
| response = "" | |
| for partial in response_stream: | |
| response = partial | |
| yield "", chat_history + [(message, response)] | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo: | |
| gr.Markdown( | |
| f""" | |
| <div style="text-align: center;"> | |
| <img src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg" width="80"/> | |
| <h1 style="margin-bottom:0;">💬 {BOT_NAME}</h1> | |
| <p style="color: gray;">Your AI business assistant — powered by Mistral-7B-Instruct</p> | |
| </div> | |
| """ | |
| ) | |
| chatbot = gr.Chatbot(height=500, label=f"Chat with {BOT_NAME}") | |
| msg = gr.Textbox(placeholder="Type your message here...", label="Your Message") | |
| clear = gr.Button("Clear Chat") | |
| state = gr.State([]) | |
| msg.submit(respond, [msg, state], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| demo.launch() | |