File size: 1,493 Bytes
2880eb4
ae75ec6
6e4965f
ae75ec6
2880eb4
 
 
 
 
 
 
ae75ec6
d3bdc78
 
4808b99
d3bdc78
 
 
 
 
 
6e4965f
d3bdc78
 
6e4965f
 
d3bdc78
 
 
 
 
2880eb4
d3bdc78
 
6e4965f
d3bdc78
 
6e4965f
d3bdc78
 
 
6e4965f
d3bdc78
ae75ec6
d3bdc78
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
import os
import gradio as gr
from mistralai import Mistral

# Fetch the API key from environment variables (set in Hugging Face secrets)
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()