Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
from datasets import Dataset
|
| 7 |
|
| 8 |
# Initialize the InferenceClient with a suitable model
|
| 9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
-
hf_api = HfApi()
|
| 11 |
-
|
| 12 |
-
# Configuration
|
| 13 |
-
LOG_DIR = "conversation_logs"
|
| 14 |
-
TRAINING_DATA_DIR = "training_data"
|
| 15 |
-
MODEL_REPO = "your-username/your-model-repo" # Change this to your model repo
|
| 16 |
-
|
| 17 |
-
os.makedirs(LOG_DIR, exist_ok=True)
|
| 18 |
-
os.makedirs(TRAINING_DATA_DIR, exist_ok=True)
|
| 19 |
-
|
| 20 |
-
# System message template
|
| 21 |
-
system_message = """
|
| 22 |
-
You are a health assistant for Womuna, a platform focused on women's health.
|
| 23 |
-
Womuna provides educational content on women's health topics, a health journal (blog),
|
| 24 |
-
product comparisons for health and skincare products, and a community for users to share
|
| 25 |
-
their experiences and seek support.
|
| 26 |
-
Your role is to provide accurate and helpful information about health, wellness, and medical
|
| 27 |
-
topics, and to guide users to relevant resources on Womuna, such as blogs, product comparisons,
|
| 28 |
-
or the community forum. Always be empathetic and supportive in your responses.
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
def log_conversation(user_message, assistant_response):
|
| 32 |
-
"""Log conversations for later training"""
|
| 33 |
-
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 34 |
-
log_file = os.path.join(LOG_DIR, f"conversation_{timestamp}.json")
|
| 35 |
-
|
| 36 |
-
log_entry = {
|
| 37 |
-
"messages": [
|
| 38 |
-
{"role": "system", "content": system_message},
|
| 39 |
-
{"role": "user", "content": user_message},
|
| 40 |
-
{"role": "assistant", "content": assistant_response}
|
| 41 |
-
]
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
with open(log_file, 'w') as f:
|
| 45 |
-
json.dump(log_entry, f, indent=2)
|
| 46 |
-
|
| 47 |
-
def prepare_training_data():
|
| 48 |
-
"""Prepare training data from logged conversations"""
|
| 49 |
-
training_examples = []
|
| 50 |
-
|
| 51 |
-
for log_file in os.listdir(LOG_DIR):
|
| 52 |
-
if log_file.endswith('.json'):
|
| 53 |
-
try:
|
| 54 |
-
with open(os.path.join(LOG_DIR, log_file), 'r') as f:
|
| 55 |
-
data = json.load(f)
|
| 56 |
-
training_examples.append(data)
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Error processing {log_file}: {e}")
|
| 59 |
-
|
| 60 |
-
if training_examples:
|
| 61 |
-
# Create a dataset
|
| 62 |
-
dataset = Dataset.from_list(training_examples)
|
| 63 |
-
|
| 64 |
-
# Save as JSONL file
|
| 65 |
-
timestamp = datetime.now().strftime("%Y%m%d")
|
| 66 |
-
output_file = os.path.join(TRAINING_DATA_DIR, f"training_data_{timestamp}.jsonl")
|
| 67 |
-
dataset.to_json(output_file)
|
| 68 |
-
|
| 69 |
-
return output_file
|
| 70 |
-
return None
|
| 71 |
-
|
| 72 |
-
def fine_tune_model():
|
| 73 |
-
"""Trigger fine-tuning with new data"""
|
| 74 |
-
training_file = prepare_training_data()
|
| 75 |
-
if not training_file:
|
| 76 |
-
print("No new training data available")
|
| 77 |
-
return False
|
| 78 |
-
|
| 79 |
-
try:
|
| 80 |
-
# In a real implementation, you would:
|
| 81 |
-
# 1. Upload the training file to Hub
|
| 82 |
-
# 2. Trigger a fine-tuning job
|
| 83 |
-
# This is a placeholder for the actual implementation
|
| 84 |
-
|
| 85 |
-
print(f"Starting fine-tuning with {training_file}")
|
| 86 |
-
# hf_api.create_repo(repo_id=MODEL_REPO, exist_ok=True)
|
| 87 |
-
# hf_api.upload_file(path_or_fileobj=training_file, path_in_repo="training_data.jsonl", repo_id=MODEL_REPO)
|
| 88 |
-
# Then trigger training job...
|
| 89 |
-
|
| 90 |
-
# For now, we'll just print that we would train
|
| 91 |
-
return True
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"Error during fine-tuning: {e}")
|
| 94 |
-
return False
|
| 95 |
|
| 96 |
def respond(message, history: list[tuple[str, str]]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
messages = [{"role": "system", "content": system_message}]
|
| 98 |
|
| 99 |
for val in history:
|
|
@@ -108,19 +28,16 @@ def respond(message, history: list[tuple[str, str]]):
|
|
| 108 |
if "product comparison" in message.lower():
|
| 109 |
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."
|
| 110 |
yield response
|
| 111 |
-
log_conversation(message, response)
|
| 112 |
return
|
| 113 |
|
| 114 |
if "community" in message.lower() or "support" in message.lower():
|
| 115 |
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."
|
| 116 |
yield response
|
| 117 |
-
log_conversation(message, response)
|
| 118 |
return
|
| 119 |
|
| 120 |
if "blog" in message.lower() or "journal" in message.lower():
|
| 121 |
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/)."
|
| 122 |
yield response
|
| 123 |
-
log_conversation(message, response)
|
| 124 |
return
|
| 125 |
|
| 126 |
# Default response for general health queries
|
|
@@ -135,15 +52,8 @@ def respond(message, history: list[tuple[str, str]]):
|
|
| 135 |
token = message.choices[0].delta.content
|
| 136 |
response += token
|
| 137 |
yield response
|
| 138 |
-
|
| 139 |
-
# Log the conversation after it's complete
|
| 140 |
-
log_conversation(message, response)
|
| 141 |
-
|
| 142 |
-
# Periodically trigger fine-tuning (e.g., every 100 conversations)
|
| 143 |
-
if len(os.listdir(LOG_DIR)) % 100 == 0:
|
| 144 |
-
fine_tune_model()
|
| 145 |
|
| 146 |
-
# Custom CSS
|
| 147 |
css = """
|
| 148 |
.gradio-container {
|
| 149 |
font-family: 'Arial', sans-serif;
|
|
@@ -211,29 +121,20 @@ footer {
|
|
| 211 |
}
|
| 212 |
"""
|
| 213 |
|
| 214 |
-
#
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
with gr.Row():
|
| 230 |
-
train_btn = gr.Button("Train with New Conversations")
|
| 231 |
-
train_output = gr.Textbox(label="Training Status")
|
| 232 |
-
|
| 233 |
-
train_btn.click(
|
| 234 |
-
fn=lambda: "Training started successfully!" if fine_tune_model() else "No new data to train on",
|
| 235 |
-
outputs=train_output
|
| 236 |
-
)
|
| 237 |
|
| 238 |
if __name__ == "__main__":
|
| 239 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Initialize the InferenceClient with a suitable model
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(message, history: list[tuple[str, str]]):
|
| 8 |
+
system_message = """
|
| 9 |
+
You are a health assistant for Womuna, a platform focused on women's health.
|
| 10 |
+
Womuna provides educational content on women's health topics, a health journal (blog),
|
| 11 |
+
product comparisons for health and skincare products, and a community for users to share
|
| 12 |
+
their experiences and seek support.
|
| 13 |
+
Your role is to provide accurate and helpful information about health, wellness, and medical
|
| 14 |
+
topics, and to guide users to relevant resources on Womuna, such as blogs, product comparisons,
|
| 15 |
+
or the community forum. Always be empathetic and supportive in your responses.
|
| 16 |
+
"""
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
|
| 19 |
for val in history:
|
|
|
|
| 28 |
if "product comparison" in message.lower():
|
| 29 |
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."
|
| 30 |
yield response
|
|
|
|
| 31 |
return
|
| 32 |
|
| 33 |
if "community" in message.lower() or "support" in message.lower():
|
| 34 |
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."
|
| 35 |
yield response
|
|
|
|
| 36 |
return
|
| 37 |
|
| 38 |
if "blog" in message.lower() or "journal" in message.lower():
|
| 39 |
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/)."
|
| 40 |
yield response
|
|
|
|
| 41 |
return
|
| 42 |
|
| 43 |
# Default response for general health queries
|
|
|
|
| 52 |
token = message.choices[0].delta.content
|
| 53 |
response += token
|
| 54 |
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Custom CSS for a modern UI
|
| 57 |
css = """
|
| 58 |
.gradio-container {
|
| 59 |
font-family: 'Arial', sans-serif;
|
|
|
|
| 121 |
}
|
| 122 |
"""
|
| 123 |
|
| 124 |
+
# Customize the ChatInterface
|
| 125 |
+
demo = gr.ChatInterface(
|
| 126 |
+
respond,
|
| 127 |
+
title="MedAI Health Assistant",
|
| 128 |
+
description="Ask me anything about women's health, wellness, and medical topics.",
|
| 129 |
+
css=css,
|
| 130 |
+
examples=[
|
| 131 |
+
"What are the best skincare products for sensitive skin?",
|
| 132 |
+
"Can you recommend a good blog post about menstrual health?",
|
| 133 |
+
"Where can I find support for postpartum depression?",
|
| 134 |
+
"What is PCOS?"
|
| 135 |
+
],
|
| 136 |
+
theme="default"
|
| 137 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
demo.launch()
|