asthaa30 commited on
Commit
bbe4abb
·
verified ·
1 Parent(s): aed7ff3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -7,6 +7,8 @@ from huggingface_hub.inference._generated.types.chat_completion import ChatCompl
7
  MODEL = "HuggingFaceH4/zephyr-7b-beta"
8
  client = InferenceClient(MODEL)
9
 
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -40,12 +42,32 @@ def respond(
40
  # Check if this is the last message in the stream
41
  if message.choices[0].finish_reason == 'stop':
42
  break
 
 
 
 
 
 
 
 
 
 
 
 
43
  else:
44
  print(f"Unexpected message type: {type(message)}")
45
  print(f"Message content: {message}")
 
 
 
 
 
46
  except Exception as e:
47
  print(f"An error occurred: {e}")
48
- yield f"An error occurred: {e}"
 
 
 
49
 
50
  # Gradio interface setup
51
  demo = gr.ChatInterface(
 
7
  MODEL = "HuggingFaceH4/zephyr-7b-beta"
8
  client = InferenceClient(MODEL)
9
 
10
+ from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionStreamOutput
11
+
12
  def respond(
13
  message,
14
  history: list[tuple[str, str]],
 
42
  # Check if this is the last message in the stream
43
  if message.choices[0].finish_reason == 'stop':
44
  break
45
+ elif isinstance(message, dict):
46
+ # Handle dictionary responses (possible API format change)
47
+ content = message.get('choices', [{}])[0].get('delta', {}).get('content')
48
+ if content:
49
+ response += content
50
+ yield response
51
+ if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
52
+ break
53
+ elif isinstance(message, str):
54
+ # Handle string responses (possible raw content)
55
+ response += message
56
+ yield response
57
  else:
58
  print(f"Unexpected message type: {type(message)}")
59
  print(f"Message content: {message}")
60
+
61
+ # Final yield to ensure all content is returned
62
+ if response:
63
+ yield response
64
+
65
  except Exception as e:
66
  print(f"An error occurred: {e}")
67
+ if response:
68
+ yield response + f"\n\nAn error occurred: {e}"
69
+ else:
70
+ yield f"An error occurred: {e}"
71
 
72
  # Gradio interface setup
73
  demo = gr.ChatInterface(