|
|
import gradio as gr |
|
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
|
|
|
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
|
|
|
def respond(message, history: list[tuple[str, str]]): |
|
|
system_message = """ |
|
|
You are a health assistant for Womuna, a platform focused on women's health. |
|
|
Womuna provides educational content on women's health topics, a health journal (blog), |
|
|
product comparisons for health and skincare products, and a community for users to share |
|
|
their experiences and seek support. |
|
|
Your role is to provide accurate and helpful information about health, wellness, and medical |
|
|
topics, and to guide users to relevant resources on Womuna, such as blogs, product comparisons, |
|
|
or the community forum. Always be empathetic and supportive in your responses. |
|
|
""" |
|
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
|
|
for val in history: |
|
|
if val[0]: |
|
|
messages.append({"role": "user", "content": val[0]}) |
|
|
if val[1]: |
|
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
if "product comparison" in message.lower(): |
|
|
response = "You can find detailed product comparisons for health and skincare products on Womuna's product comparison section. Visit [Womuna Product Comparisons](https://womuna.com/shop/) to make informed buying decisions." |
|
|
yield response |
|
|
return |
|
|
|
|
|
if "community" in message.lower() or "support" in message.lower(): |
|
|
response = "Womuna has a built-in community where you can share your experiences, seek advice, and get moral support from other users. Visit [Womuna Community](https://womuna.com/womunity/) to join the conversation." |
|
|
yield response |
|
|
return |
|
|
|
|
|
if "blog" in message.lower() or "journal" in message.lower(): |
|
|
response = "Womuna's health journal offers a wealth of educational content on women's health topics. Check out the latest posts at [Womuna Blog](https://womuna.com/)." |
|
|
yield response |
|
|
return |
|
|
|
|
|
|
|
|
response = "" |
|
|
for message in client.chat_completion( |
|
|
messages, |
|
|
max_tokens=512, |
|
|
stream=True, |
|
|
temperature=0.7, |
|
|
top_p=0.95, |
|
|
): |
|
|
token = message.choices[0].delta.content |
|
|
response += token |
|
|
yield response |
|
|
|
|
|
|
|
|
css = """ |
|
|
.gradio-container { |
|
|
font-family: 'Arial', sans-serif; |
|
|
background: linear-gradient(135deg, #f9f9f9, #e0e0e0); |
|
|
color: #333; |
|
|
padding: 20px; |
|
|
border-radius: 15px; |
|
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
|
} |
|
|
/* Chat container */ |
|
|
.chat-container { |
|
|
background: white; |
|
|
border-radius: 10px; |
|
|
padding: 20px; |
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); |
|
|
} |
|
|
/* User message bubble */ |
|
|
.user-message { |
|
|
background: #007bff; |
|
|
color: white; |
|
|
border-radius: 10px 10px 0 10px; |
|
|
padding: 10px; |
|
|
margin: 5px 0; |
|
|
max-width: 70%; |
|
|
align-self: flex-end; |
|
|
} |
|
|
/* Bot message bubble */ |
|
|
.bot-message { |
|
|
background: #f1f1f1; |
|
|
color: #333; |
|
|
border-radius: 10px 10px 10px 0; |
|
|
padding: 10px; |
|
|
margin: 5px 0; |
|
|
max-width: 70%; |
|
|
align-self: flex-start; |
|
|
} |
|
|
/* Input box */ |
|
|
.input-box { |
|
|
border-radius: 10px; |
|
|
border: 1px solid #ddd; |
|
|
padding: 10px; |
|
|
width: 100%; |
|
|
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1); |
|
|
} |
|
|
/* Submit button */ |
|
|
.submit-button { |
|
|
background: #007bff; |
|
|
color: white; |
|
|
border: none; |
|
|
border-radius: 10px; |
|
|
padding: 10px 20px; |
|
|
cursor: pointer; |
|
|
transition: background 0.3s ease; |
|
|
} |
|
|
.submit-button:hover { |
|
|
background: #0056b3; |
|
|
} |
|
|
/* Hide the footer */ |
|
|
footer { |
|
|
display: none !important; |
|
|
} |
|
|
/* Hide the "Flag" button */ |
|
|
.gr-button { |
|
|
display: none !important; |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
demo = gr.ChatInterface( |
|
|
respond, |
|
|
title="MedAI Health Assistant", |
|
|
description="Ask me anything about women's health, wellness, and medical topics.", |
|
|
css=css, |
|
|
examples=[ |
|
|
"What are the best skincare products for sensitive skin?", |
|
|
"Can you recommend a good blog post about menstrual health?", |
|
|
"Where can I find support for postpartum depression?", |
|
|
"What is PCOS?" |
|
|
], |
|
|
theme="default" |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|