Rajan Sharma commited on
Commit
b3902ee
·
verified ·
1 Parent(s): 2bdb6e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -58,6 +58,18 @@ def is_identity_query(message, history):
58
  return True
59
  return False
60
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # -------------------
62
  # Cohere Hosted
63
  # -------------------
@@ -65,31 +77,24 @@ _co_client = None
65
  if USE_HOSTED_COHERE:
66
  _co_client = cohere.Client(api_key=COHERE_API_KEY)
67
 
68
- def _cohere_parse(resp):
69
- if hasattr(resp, "output_text") and resp.output_text:
70
- return resp.output_text.strip()
71
- if getattr(resp, "message", None) and getattr(resp.message, "content", None):
72
- for p in resp.message.content:
73
- if hasattr(p, "text") and p.text:
74
- return p.text.strip()
75
- if hasattr(resp, "text") and resp.text:
76
- return resp.text.strip()
77
- return "Sorry, I couldn't parse the response from Cohere."
78
-
79
  def cohere_chat(message, history):
80
  try:
81
- msgs = []
82
- for u, a in (history or []):
83
- msgs.append({"role": "user", "content": u})
84
- msgs.append({"role": "assistant", "content": a})
85
- msgs.append({"role": "user", "content": message})
86
- resp = _co_client.responses.create(
87
  model="command-r7b-12-2024",
88
- messages=msgs,
89
  temperature=0.3,
90
  max_tokens=350,
91
  )
92
- return _cohere_parse(resp)
 
 
 
 
 
 
 
93
  except Exception as e:
94
  return f"Error calling Cohere API: {e}"
95
 
@@ -99,9 +104,7 @@ def cohere_chat(message, history):
99
  @lru_cache(maxsize=1)
100
  def load_local_model():
101
  if not HF_TOKEN:
102
- raise RuntimeError(
103
- "HUGGINGFACE_HUB_TOKEN is not set."
104
- )
105
  login(token=HF_TOKEN, add_to_git_credential=False)
106
  dtype, device_map = pick_dtype_and_map()
107
  tok = AutoTokenizer.from_pretrained(
@@ -166,7 +169,7 @@ def chat_fn(message, history, user_tz):
166
  return f"Error: {e}"
167
 
168
  # -------------------
169
- # Theme & CSS
170
  # -------------------
171
  theme = gr.themes.Soft(
172
  primary_hue="teal",
@@ -196,6 +199,11 @@ h1 {
196
  background: var(--brand-accent) !important;
197
  color: var(--brand-text-light) !important;
198
  }
 
 
 
 
 
199
  """
200
 
201
  # -------------------
 
58
  return True
59
  return False
60
 
61
+ # Build a simple prompt string for the older/stable Cohere chat API.
62
+ def _history_to_prompt(message, history):
63
+ parts = []
64
+ for u, a in (history or []):
65
+ if u:
66
+ parts.append(f"User: {u}")
67
+ if a:
68
+ parts.append(f"Assistant: {a}")
69
+ parts.append(f"User: {message}")
70
+ parts.append("Assistant:")
71
+ return "\n".join(parts)
72
+
73
  # -------------------
74
  # Cohere Hosted
75
  # -------------------
 
77
  if USE_HOSTED_COHERE:
78
  _co_client = cohere.Client(api_key=COHERE_API_KEY)
79
 
 
 
 
 
 
 
 
 
 
 
 
80
  def cohere_chat(message, history):
81
  try:
82
+ # Use the stable chat() API to avoid SDK breaking changes.
83
+ prompt = _history_to_prompt(message, history)
84
+ resp = _co_client.chat(
 
 
 
85
  model="command-r7b-12-2024",
86
+ message=prompt,
87
  temperature=0.3,
88
  max_tokens=350,
89
  )
90
+ # Newer SDKs provide .text; older ones may use .reply or generations
91
+ if hasattr(resp, "text") and resp.text:
92
+ return resp.text.strip()
93
+ if hasattr(resp, "reply") and resp.reply:
94
+ return resp.reply.strip()
95
+ if hasattr(resp, "generations") and resp.generations:
96
+ return resp.generations[0].text.strip()
97
+ return "Sorry, I couldn't parse the response from Cohere."
98
  except Exception as e:
99
  return f"Error calling Cohere API: {e}"
100
 
 
104
  @lru_cache(maxsize=1)
105
  def load_local_model():
106
  if not HF_TOKEN:
107
+ raise RuntimeError("HUGGINGFACE_HUB_TOKEN is not set.")
 
 
108
  login(token=HF_TOKEN, add_to_git_credential=False)
109
  dtype, device_map = pick_dtype_and_map()
110
  tok = AutoTokenizer.from_pretrained(
 
169
  return f"Error: {e}"
170
 
171
  # -------------------
172
+ # Theme & CSS (both bot and user bubbles teal + white text)
173
  # -------------------
174
  theme = gr.themes.Soft(
175
  primary_hue="teal",
 
199
  background: var(--brand-accent) !important;
200
  color: var(--brand-text-light) !important;
201
  }
202
+
203
+ /* Inputs a bit softer */
204
+ textarea, input, .gr-input {
205
+ border-radius: 12px !important;
206
+ }
207
  """
208
 
209
  # -------------------