amasood commited on
Commit
2880eb4
·
verified ·
1 Parent(s): ab25214

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -1,38 +1,38 @@
 
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="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
 
 
1
+ import os
2
  import gradio as gr
3
  from mistralai import Mistral
4
 
5
+ # Fetch the API key from environment variables (set in Hugging Face secrets)
6
+ api_key = os.getenv("MISTRAL_API_KEY")
7
+
8
+ if not api_key:
9
+ raise ValueError("⚠️ Missing MISTRAL_API_KEY. Please set it in the Space settings.")
10
+
11
+ client = Mistral(api_key=api_key)
12
 
13
  def chat_with_mistral(user_input, history):
14
  """Handles chat with Mistral LLM."""
15
  try:
 
16
  messages = [{"role": "system", "content": "You are a helpful medical assistant specialized in skin and acne care."}]
17
  for human, ai in history:
18
  messages.append({"role": "user", "content": human})
19
  messages.append({"role": "assistant", "content": ai})
20
  messages.append({"role": "user", "content": user_input})
21
 
 
22
  response = client.chat.complete(
23
  model="mistral-medium",
24
  messages=messages
25
  )
26
 
 
27
  reply = response.choices[0].message.content
28
  history.append((user_input, reply))
29
  return history, ""
30
 
31
  except Exception as e:
32
+ error_msg = f"⚠️ API error occurred: {str(e)}"
33
  history.append((user_input, error_msg))
34
  return history, ""
35
 
 
36
  with gr.Blocks() as demo:
37
  gr.Markdown("## 💬 Mistral Chatbot — Skin & Acne Specialist")
38