|
|
import os |
|
|
import gradio as gr |
|
|
from mistralai import Mistral |
|
|
|
|
|
|
|
|
api_key = os.getenv("MISTRAL_API_KEY") |
|
|
|
|
|
if not api_key: |
|
|
raise ValueError("⚠️ Missing MISTRAL_API_KEY. Please set it in the Space settings.") |
|
|
|
|
|
client = Mistral(api_key=api_key) |
|
|
|
|
|
def chat_with_mistral(user_input, history): |
|
|
"""Handles chat with Mistral LLM.""" |
|
|
try: |
|
|
messages = [{"role": "system", "content": "You are a helpful medical assistant specialized in skin and acne care."}] |
|
|
for human, ai in history: |
|
|
messages.append({"role": "user", "content": human}) |
|
|
messages.append({"role": "assistant", "content": ai}) |
|
|
messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
response = client.chat.complete( |
|
|
model="mistral-medium", |
|
|
messages=messages |
|
|
) |
|
|
|
|
|
reply = response.choices[0].message.content |
|
|
history.append((user_input, reply)) |
|
|
return history, "" |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"⚠️ API error occurred: {str(e)}" |
|
|
history.append((user_input, error_msg)) |
|
|
return history, "" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## 💬 Mistral Chatbot — Skin & Acne Specialist") |
|
|
|
|
|
chatbot = gr.Chatbot(height=500) |
|
|
msg = gr.Textbox(label="Ask your question here:") |
|
|
clear = gr.ClearButton([msg, chatbot]) |
|
|
|
|
|
msg.submit(chat_with_mistral, [msg, chatbot], [chatbot, msg]) |
|
|
|
|
|
demo.launch() |
|
|
|