asthaa30 commited on
Commit
6fae060
·
verified ·
1 Parent(s): 6cb707e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -11,6 +11,8 @@ MODEL = "nomiChroma3.1"
11
 
12
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
 
 
 
14
  def respond(
15
  message,
16
  history: list[tuple[str, str]],
@@ -35,21 +37,38 @@ def respond(
35
  temperature=temperature,
36
  top_p=top_p,
37
  ):
38
- try:
39
- # Try to access the content directly
40
- token = message.choices[0].delta.content
41
- except AttributeError:
42
- # If above fails, the response might be in a different format
43
- # Print the raw message for debugging
44
- print(f"Debug - Raw message: {message}")
45
- # Try to parse as JSON
46
  try:
47
- parsed_message = json.loads(str(message))
 
48
  token = parsed_message.get('choices', [{}])[0].get('delta', {}).get('content', '')
49
  except json.JSONDecodeError as e:
50
  print(f"JSON Decode Error: {e}")
51
- print(f"Problematic message: {message}")
52
- token = "" # or handle this case as appropriate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  response += token
54
  yield response
55
  except Exception as e:
 
11
 
12
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
 
14
+ import json
15
+
16
  def respond(
17
  message,
18
  history: list[tuple[str, str]],
 
37
  temperature=temperature,
38
  top_p=top_p,
39
  ):
40
+ print(f"Debug - Raw message type: {type(message)}")
41
+ print(f"Debug - Raw message content: {message}")
42
+
43
+ if isinstance(message, str):
44
+ # If the message is already a string, try to parse it as JSON
 
 
 
45
  try:
46
+ parsed_message = json.loads(message)
47
+ print(f"Debug - Parsed JSON: {parsed_message}")
48
  token = parsed_message.get('choices', [{}])[0].get('delta', {}).get('content', '')
49
  except json.JSONDecodeError as e:
50
  print(f"JSON Decode Error: {e}")
51
+ print(f"Problematic message: {message[:100]}...") # Print first 100 chars
52
+ if message.startswith('data:'):
53
+ # If the message starts with 'data:', it might be SSE format
54
+ clean_message = message.lstrip('data:').strip()
55
+ try:
56
+ parsed_message = json.loads(clean_message)
57
+ print(f"Debug - Parsed JSON after cleaning: {parsed_message}")
58
+ token = parsed_message.get('choices', [{}])[0].get('delta', {}).get('content', '')
59
+ except json.JSONDecodeError as e:
60
+ print(f"JSON Decode Error after cleaning: {e}")
61
+ token = ""
62
+ else:
63
+ token = message # Use the raw message as the token if parsing fails
64
+ else:
65
+ # If it's not a string, it might be an object with attributes
66
+ try:
67
+ token = message.choices[0].delta.content
68
+ except AttributeError:
69
+ print(f"Attribute Error - message structure: {dir(message)}")
70
+ token = str(message) # Fallback to string representation
71
+
72
  response += token
73
  yield response
74
  except Exception as e: