amasood commited on
Commit
d3bdc78
·
verified ·
1 Parent(s): c38c796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -52
app.py CHANGED
@@ -1,64 +1,45 @@
1
  import gradio as gr
2
  from mistralai import Mistral
3
- import os
4
 
5
- # -----------------------------
6
- # 1. Initialize Mistral client
7
- # -----------------------------
8
- MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
9
-
10
- if not MISTRAL_API_KEY:
11
- raise ValueError("Please set your MISTRAL_API_KEY in Hugging Face Space secrets.")
12
-
13
- client = Mistral(api_key=MISTRAL_API_KEY)
14
-
15
- # -----------------------------
16
- # 2. Chatbot function
17
- # -----------------------------
18
- def chat_with_mistral(history, user_input):
19
- """
20
- Send conversation to Mistral API and return the model's response.
21
- """
22
- # Prepare messages from chat history
23
- messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
24
- for msg in history:
25
- messages.append({"role": "user", "content": msg[0]})
26
- messages.append({"role": "assistant", "content": msg[1]})
27
- messages.append({"role": "user", "content": user_input})
28
 
 
 
29
  try:
 
 
 
 
 
 
 
 
30
  response = client.chat.complete(
31
- model="mistral-large-latest", # Mistral's latest large language model
32
- messages=messages,
33
  )
34
- bot_reply = response.choices[0].message["content"]
35
- except Exception as e:
36
- bot_reply = f"⚠️ Error: {str(e)}"
37
 
38
- return bot_reply
39
-
40
- # -----------------------------
41
- # 3. Gradio Interface
42
- # -----------------------------
43
- def chat_interface(user_input, history):
44
- reply = chat_with_mistral(history, user_input)
45
- history.append((user_input, reply))
46
- return history, history
47
-
48
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
49
- gr.Markdown("# 💬 Mistral-Powered Chatbot")
50
- gr.Markdown("Chat with an AI assistant powered by **Mistral** and **Gradio**.")
51
-
52
- chatbot = gr.Chatbot(label="Mistral Chatbot", height=500)
53
- user_input = gr.Textbox(label="Your message", placeholder="Type your question here...")
54
- clear_btn = gr.Button("Clear Chat")
55
 
56
- state = gr.State([])
 
 
57
 
58
- user_input.submit(chat_interface, [user_input, state], [chatbot, state])
59
- clear_btn.click(lambda: ([], []), None, [chatbot, state])
 
60
 
61
- gr.Markdown("🧠 Model: **mistral-large-latest** | ⚙️ Built with Gradio")
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
  from mistralai import Mistral
 
3
 
4
+ # Initialize Mistral client with your API key
5
+ # Set your API key in the Hugging Face Space secret settings
6
+ # (Settings → Variables → Add "MISTRAL_API_KEY")
7
+ client = Mistral(api_key="YOUR_MISTRAL_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def chat_with_mistral(user_input, history):
10
+ """Handles chat with Mistral LLM."""
11
  try:
12
+ # Convert chat history to message format
13
+ messages = [{"role": "system", "content": "You are a helpful medical assistant specialized in skin and acne care."}]
14
+ for human, ai in history:
15
+ messages.append({"role": "user", "content": human})
16
+ messages.append({"role": "assistant", "content": ai})
17
+ messages.append({"role": "user", "content": user_input})
18
+
19
+ # Query Mistral API
20
  response = client.chat.complete(
21
+ model="mistral-medium",
22
+ messages=messages
23
  )
 
 
 
24
 
25
+ # ✅ Correct way to extract text
26
+ reply = response.choices[0].message.content
27
+ history.append((user_input, reply))
28
+ return history, ""
29
+
30
+ except Exception as e:
31
+ error_msg = f"⚠️ Error: {str(e)}"
32
+ history.append((user_input, error_msg))
33
+ return history, ""
 
 
 
 
 
 
 
 
34
 
35
+ # Gradio interface
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("## 💬 Mistral Chatbot — Skin & Acne Specialist")
38
 
39
+ chatbot = gr.Chatbot(height=500)
40
+ msg = gr.Textbox(label="Ask your question here:")
41
+ clear = gr.ClearButton([msg, chatbot])
42
 
43
+ msg.submit(chat_with_mistral, [msg, chatbot], [chatbot, msg])
44
 
45
+ demo.launch()