Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,128 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import os # Import os to potentially get token from environment
|
4 |
+
|
5 |
+
"""
|
6 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
7 |
+
"""
|
8 |
+
# !! REPLACE THIS WITH YOUR HUGGING FACE MODEL ID !!
|
9 |
+
MODEL_ID = "drwlf/PsychoQwen14b"
|
10 |
+
# It's recommended to use HF_TOKEN from environment/secrets
|
11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
+
|
13 |
+
# Initialize client, handle potential missing token
|
14 |
+
try:
|
15 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
16 |
+
print("InferenceClient initialized successfully.")
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error initializing InferenceClient: {e}")
|
19 |
+
print("Please ensure HF_TOKEN is set in your environment/secrets.")
|
20 |
+
client = None # Set client to None if initialization fails
|
21 |
+
|
22 |
+
|
23 |
+
def respond(
|
24 |
+
message,
|
25 |
+
history: list[tuple[str, str]],
|
26 |
+
system_message,
|
27 |
+
max_tokens,
|
28 |
+
temperature,
|
29 |
+
top_p,
|
30 |
+
top_k # Added top_k parameter
|
31 |
+
):
|
32 |
+
"""
|
33 |
+
Generator function to stream responses from the HF Inference API.
|
34 |
+
"""
|
35 |
+
if not client:
|
36 |
+
yield "Error: Inference Client not initialized. Check HF_TOKEN."
|
37 |
+
return
|
38 |
+
|
39 |
+
messages = [{"role": "system", "content": system_message}]
|
40 |
+
|
41 |
+
for val in history:
|
42 |
+
if val[0]:
|
43 |
+
messages.append({"role": "user", "content": val[0]})
|
44 |
+
if val[1]:
|
45 |
+
messages.append({"role": "assistant", "content": val[1]})
|
46 |
+
|
47 |
+
messages.append({"role": "user", "content": message})
|
48 |
+
|
49 |
+
response = ""
|
50 |
+
stream = None # Initialize stream variable
|
51 |
+
|
52 |
+
# Handle Top-K value (API often expects None to disable, not 0)
|
53 |
+
top_k_val = top_k if top_k > 0 else None
|
54 |
+
|
55 |
+
try:
|
56 |
+
stream = client.chat_completion(
|
57 |
+
messages,
|
58 |
+
max_tokens=max_tokens,
|
59 |
+
stream=True,
|
60 |
+
temperature=temperature,
|
61 |
+
top_p=top_p,
|
62 |
+
top_k=top_k_val # Pass the adjusted top_k value
|
63 |
+
)
|
64 |
+
|
65 |
+
for message_chunk in stream:
|
66 |
+
# Check for content and delta before accessing
|
67 |
+
if (hasattr(message_chunk, 'choices') and
|
68 |
+
len(message_chunk.choices) > 0 and
|
69 |
+
hasattr(message_chunk.choices[0], 'delta') and
|
70 |
+
message_chunk.choices[0].delta and
|
71 |
+
hasattr(message_chunk.choices[0].delta, 'content')):
|
72 |
+
|
73 |
+
token = message_chunk.choices[0].delta.content
|
74 |
+
if token: # Ensure token is not None or empty
|
75 |
+
response += token
|
76 |
+
yield response
|
77 |
+
# Optional: Add error checking within the loop if needed
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error during chat completion: {e}")
|
81 |
+
yield f"Sorry, an error occurred: {str(e)}"
|
82 |
+
finally:
|
83 |
+
# Ensure the stream object is properly handled if it exists
|
84 |
+
# (Though InferenceClient might handle cleanup internally)
|
85 |
+
if stream is not None:
|
86 |
+
# Potential cleanup if required by the library, often not needed explicitly
|
87 |
+
pass
|
88 |
+
|
89 |
+
|
90 |
+
"""
|
91 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
92 |
+
"""
|
93 |
+
demo = gr.ChatInterface(
|
94 |
+
respond,
|
95 |
+
chatbot=gr.Chatbot(height=500), # Set chatbot height
|
96 |
+
additional_inputs=[
|
97 |
+
gr.Textbox(value="You are a friendly psychotherapy AI capable of thinking.", label="System message"),
|
98 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
99 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"), # Adjusted max temp based on common usage
|
100 |
+
gr.Slider(
|
101 |
+
minimum=0.05, # Min Top-P often > 0
|
102 |
+
maximum=1.0,
|
103 |
+
value=0.95,
|
104 |
+
step=0.05,
|
105 |
+
label="Top-P (nucleus sampling)",
|
106 |
+
),
|
107 |
+
# Added Top-K slider
|
108 |
+
gr.Slider(
|
109 |
+
minimum=0, # 0 disables Top-K
|
110 |
+
maximum=100, # Common range, adjust if needed
|
111 |
+
value=0, # Default to disabled
|
112 |
+
step=1,
|
113 |
+
label="Top-K (0 = disabled)",
|
114 |
+
),
|
115 |
+
],
|
116 |
+
title="PsychoQwen Chat",
|
117 |
+
description=f"Chat with {MODEL_ID}. Adjust generation parameters below.",
|
118 |
+
retry_btn="Retry",
|
119 |
+
undo_btn="Undo",
|
120 |
+
clear_btn="Clear Chat",
|
121 |
+
)
|
122 |
+
|
123 |
+
|
124 |
+
if __name__ == "__main__":
|
125 |
+
demo.queue().launch(debug=True) # Add queue() for streaming
|
126 |
|
127 |
"""
|
128 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|