Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
for
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
token =
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Import the Carmen module. (Ensure that the repository is installed and accessible.)
|
| 5 |
+
from carmen.sentience import analyze_sentience
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Initialize the chat client.
|
| 8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
| 10 |
+
def chat_and_sentience(message, history, system_message, max_tokens, temperature, top_p):
|
| 11 |
+
# Prepare messages for the LLM conversation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
+
for user_msg, assistant_msg in history:
|
| 14 |
+
if user_msg:
|
| 15 |
+
messages.append({"role": "user", "content": user_msg})
|
| 16 |
+
if assistant_msg:
|
| 17 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
+
|
| 20 |
response = ""
|
| 21 |
+
# Generate chat response via streaming.
|
| 22 |
+
for chat in client.chat_completion(
|
| 23 |
messages,
|
| 24 |
max_tokens=max_tokens,
|
| 25 |
stream=True,
|
| 26 |
temperature=temperature,
|
| 27 |
top_p=top_p,
|
| 28 |
):
|
| 29 |
+
token = chat.choices[0].delta.content
|
|
|
|
| 30 |
response += token
|
| 31 |
+
# Update the UI with the intermediate chat history; sentiment analysis hasn't run yet.
|
| 32 |
+
yield [history + [(message, response)], None]
|
| 33 |
+
|
| 34 |
+
# Once the full response is assembled, perform sentience analysis using Carmen.
|
| 35 |
+
# The function analyze_sentience is assumed to return a dictionary or list of sentiment scores/labels.
|
| 36 |
+
sentiment_results = analyze_sentience(response)
|
| 37 |
+
|
| 38 |
+
# Format the results for display. Adjust the formatting based on the actual output of analyze_sentience.
|
| 39 |
+
if isinstance(sentiment_results, dict):
|
| 40 |
+
sentiment_str = "\n".join([f"{k}: {v:.2f}" for k, v in sentiment_results.items()])
|
| 41 |
+
elif isinstance(sentiment_results, list):
|
| 42 |
+
sentiment_str = "\n".join([f"{item['label']}: {item['score']:.2f}" for item in sentiment_results])
|
| 43 |
+
else:
|
| 44 |
+
sentiment_str = str(sentiment_results)
|
| 45 |
+
|
| 46 |
+
# Yield the final state: updated chat history and the sentiment analysis result.
|
| 47 |
+
yield [history + [(message, response)], sentiment_str]
|
| 48 |
|
| 49 |
+
# Build the UI with gr.Blocks.
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
with gr.Row():
|
| 52 |
+
chatbot = gr.Chatbot(label="Chat")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
sentiment_box = gr.Textbox(
|
| 55 |
+
label="Sentience Moment Scanner",
|
| 56 |
+
lines=4,
|
| 57 |
+
placeholder="Emotion analysis will appear here..."
|
| 58 |
+
)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
message_input = gr.Textbox(label="Your Message")
|
| 61 |
+
with gr.Row():
|
| 62 |
+
system_message_input = gr.Textbox(value="You are a friendly Chatbot.", label="System Message")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
| 65 |
+
with gr.Row():
|
| 66 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 69 |
+
submit_btn = gr.Button("Send")
|
| 70 |
|
| 71 |
+
# Use a state to track conversation history.
|
| 72 |
+
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Wire up the events: on click or pressing enter the chat and sentiment analysis runs.
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
chat_and_sentience,
|
| 77 |
+
inputs=[message_input, state, system_message_input, max_tokens_slider, temperature_slider, top_p_slider],
|
| 78 |
+
outputs=[chatbot, sentiment_box],
|
| 79 |
+
show_progress=True
|
| 80 |
+
)
|
| 81 |
+
message_input.submit(
|
| 82 |
+
chat_and_sentience,
|
| 83 |
+
inputs=[message_input, state, system_message_input, max_tokens_slider, temperature_slider, top_p_slider],
|
| 84 |
+
outputs=[chatbot, sentiment_box],
|
| 85 |
+
show_progress=True
|
| 86 |
+
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch()
|