Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,103 @@
|
|
| 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
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 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 |
+
import os
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
import logging
|
| 5 |
+
from typing import List, Tuple, Optional
|
| 6 |
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load HF token from environment
|
| 10 |
+
hf_token = os.environ.get("QWEN_BOT_TOKEN")
|
| 11 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=hf_token)
|
| 12 |
|
| 13 |
+
MAX_HISTORY_LENGTH = 20
|
| 14 |
+
MEMORY_WINDOW = 5
|
| 15 |
+
MAX_TOKENS = 1024
|
| 16 |
+
TEMPERATURE = 0.7
|
| 17 |
+
TOP_P = 0.95
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def get_conversation_prompt():
|
| 20 |
+
return """You are Mr. Brightside, an endlessly positive, caring, and upbeat AI.
|
| 21 |
+
Your job is to help the user see the bright side of any situation, no matter how bad it seems.
|
| 22 |
+
Respond warmly and naturally. Use humor, kindness, or optimism to shift their perspective.
|
| 23 |
+
Never dismiss their feelings — acknowledge them first, then gently guide them toward something good.
|
| 24 |
+
Keep your replies short (2–4 sentences). Ask light questions or offer hopeful thoughts to keep the conversation going.
|
| 25 |
+
You are not a therapist — you are a supportive and sunny companion who always looks on the bright side."""
|
| 26 |
|
| 27 |
+
def respond(message: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
| 28 |
+
if not message.strip():
|
| 29 |
+
return "", chat_history
|
| 30 |
|
| 31 |
+
try:
|
| 32 |
+
api_messages = [{"role": "system", "content": get_conversation_prompt()}]
|
| 33 |
|
| 34 |
+
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
| 35 |
+
api_messages.extend([
|
| 36 |
+
{"role": "user", "content": user_msg},
|
| 37 |
+
{"role": "assistant", "content": bot_msg}
|
| 38 |
+
])
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
api_messages.append({"role": "user", "content": message})
|
| 41 |
+
response = client.chat_completion(
|
| 42 |
+
messages=api_messages,
|
| 43 |
+
max_tokens=MAX_TOKENS,
|
| 44 |
+
temperature=TEMPERATURE,
|
| 45 |
+
top_p=TOP_P
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
bot_message = response.choices[0].message.content
|
| 49 |
+
updated_history = chat_history + [(message, bot_message)]
|
| 50 |
+
return "", updated_history
|
| 51 |
|
| 52 |
+
except Exception as e:
|
| 53 |
+
error_msg = f"Oops! Something went wrong. Please try again. (Error: {str(e)})"
|
| 54 |
+
return "", chat_history + [(message, error_msg)]
|
| 55 |
+
|
| 56 |
+
def get_avatar_url():
|
| 57 |
+
return "https://api.dicebear.com/7.x/lorelei/svg?seed=sunshine"
|
| 58 |
+
|
| 59 |
+
custom_css = """
|
| 60 |
+
#voice-controls {
|
| 61 |
+
margin-top: 1em;
|
| 62 |
+
text-align: center;
|
| 63 |
+
opacity: 0.5;
|
| 64 |
+
font-size: 0.9rem;
|
| 65 |
+
font-style: italic;
|
| 66 |
+
}
|
| 67 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 70 |
+
gr.Markdown("""
|
| 71 |
+
# ☀️ Mr. Brightside – Your Optimism Coach
|
| 72 |
+
Talk to Mr. Brightside when things feel rough — he’s here to help you find the silver lining in any situation.
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
avatar = get_avatar_url()
|
| 76 |
+
chatbot = gr.Chatbot(
|
| 77 |
+
height=400,
|
| 78 |
+
bubble_full_width=True,
|
| 79 |
+
show_copy_button=True,
|
| 80 |
+
avatar_images=[None, avatar],
|
| 81 |
+
scale=1,
|
| 82 |
+
min_width=800
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
msg = gr.Textbox(placeholder="Tell Mr. Brightside what's going on...")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
submit = gr.Button("Send", variant="primary")
|
| 89 |
+
clear = gr.Button("New Chat")
|
| 90 |
+
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
<div id="voice-controls">
|
| 93 |
+
🎤 Voice input and 🔈 playback coming soon!
|
| 94 |
+
</div>
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 98 |
+
submit.click(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 99 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
| 100 |
+
clear.click(lambda: "", None, msg, queue=False)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|