Update app.py
Browse filesagenten eingeführt
app.py
CHANGED
|
@@ -1,12 +1,36 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
|
|
|
| 3 |
import PyPDF2
|
| 4 |
-
import datetime
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
pdf_path = "Einsteiger Guide Leben im Einklang mit dem weiblichen Zyklus.pdf"
|
| 11 |
pdf_reader = PyPDF2.PdfReader(pdf_path)
|
| 12 |
pdf_text = ""
|
|
@@ -14,15 +38,27 @@ for page_num in range(len(pdf_reader.pages)):
|
|
| 14 |
page = pdf_reader.pages[page_num]
|
| 15 |
pdf_text += page.extract_text()
|
| 16 |
|
| 17 |
-
# Define the CSS style for the UI with a mobile-friendly design
|
| 18 |
st.markdown("""
|
| 19 |
<style>
|
| 20 |
body {
|
| 21 |
font-family: 'Satoshi', sans-serif;
|
| 22 |
background-color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
.main {
|
| 25 |
background-color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
.stButton>button {
|
| 28 |
background-color: #ff4d94;
|
|
@@ -32,10 +68,14 @@ st.markdown("""
|
|
| 32 |
.stTextInput>div>div>input {
|
| 33 |
background-color: #ff99c2;
|
| 34 |
color: white;
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
.stTextArea>div>textarea {
|
| 37 |
background-color: #ff99c2;
|
| 38 |
color: white;
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
.stSubheader, .stText, .stHeader, h1, h2 {
|
| 41 |
color: #b30047;
|
|
@@ -62,69 +102,39 @@ st.markdown("""
|
|
| 62 |
""", unsafe_allow_html=True)
|
| 63 |
|
| 64 |
# Set title and subtitle
|
| 65 |
-
st.title("
|
| 66 |
-
st.subheader("
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
days_cycle = st.number_input("Days of Cycle", min_value=21, max_value=35, step=1)
|
| 71 |
-
last_period_start_date = st.date_input("Last Period Start Date")
|
| 72 |
-
mood_past_3_days = st.text_area("Mood of the Past 3 Days")
|
| 73 |
-
goals_next_3_months = st.text_area("Goals for the Next 3 Months")
|
| 74 |
-
|
| 75 |
-
# Function to determine the current cycle phase
|
| 76 |
-
def determine_cycle_phase(days_menstruation, days_cycle, last_period_start_date):
|
| 77 |
-
today = datetime.date.today()
|
| 78 |
-
cycle_day = (today - last_period_start_date).days % days_cycle
|
| 79 |
-
|
| 80 |
-
if cycle_day < days_menstruation:
|
| 81 |
-
return "Menstrual Phase (Winter)"
|
| 82 |
-
elif cycle_day < days_menstruation + (days_cycle - days_menstruation) // 2:
|
| 83 |
-
return "Follicular Phase (Spring)"
|
| 84 |
-
elif cycle_day < days_cycle - (days_cycle - days_menstruation) // 2:
|
| 85 |
-
return "Ovulation Phase (Summer)"
|
| 86 |
-
else:
|
| 87 |
-
return "Luteal Phase (Autumn)"
|
| 88 |
|
| 89 |
# Submit button
|
| 90 |
-
if st.button("
|
| 91 |
-
if
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
st.write(phase_info)
|
| 105 |
-
|
| 106 |
-
# Knowledge base information
|
| 107 |
-
st.header("Menstrual Cycle Knowledge Base")
|
| 108 |
-
st.write("""
|
| 109 |
-
The menstrual cycle is a natural process that occurs in the female body to prepare for a potential pregnancy. It consists of several phases:
|
| 110 |
-
- **Menstrual Phase:** This is the phase where the uterine lining sheds, causing bleeding.
|
| 111 |
-
- **Follicular Phase:** The body prepares for ovulation by growing follicles in the ovaries.
|
| 112 |
-
- **Ovulation Phase:** An egg is released from the ovary, making it the most fertile phase.
|
| 113 |
-
- **Luteal Phase:** The body prepares for a possible pregnancy or the next menstrual cycle.
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
- **Ovulation Phase:** Use your high energy for social and professional networking.
|
| 119 |
-
- **Luteal Phase:** Focus on completing tasks and integrate stress-reducing activities like yoga.
|
| 120 |
|
| 121 |
-
|
| 122 |
-
""
|
|
|
|
| 123 |
else:
|
| 124 |
-
st.error("
|
| 125 |
|
| 126 |
-
st.header("
|
| 127 |
st.write("""
|
| 128 |
-
|
| 129 |
-
Greetings Nur M.
|
| 130 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
import PyPDF2
|
|
|
|
| 5 |
|
| 6 |
+
# Initialisiere die Text-Generierungs-Pipeline für verschiedene Kapitel
|
| 7 |
+
kapitel_agenten = {
|
| 8 |
+
"Einführung in die weibliche Anatomie": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 9 |
+
"Der Menstruationszyklus": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 10 |
+
"Ernährung und Lebensstil": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 11 |
+
"Stressmanagement und Schlaf": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 12 |
+
"Umweltgifte und hormonelle Gesundheit": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 13 |
+
"Empfohlene Tees und Kräuter": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 14 |
+
"Weitere Tipps und Strategien": pipeline('text-generation', model='distilgpt2', framework='pt'),
|
| 15 |
+
"Zusammenfassung und Ausblick": pipeline('text-generation', model='distilgpt2', framework='pt')
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
# Intent-Detection-Modell initialisieren
|
| 19 |
+
intent_model_name = "bert-base-uncased"
|
| 20 |
+
intent_model = AutoModelForSequenceClassification.from_pretrained(intent_model_name)
|
| 21 |
+
intent_tokenizer = AutoTokenizer.from_pretrained(intent_model_name)
|
| 22 |
+
|
| 23 |
+
intents = ["Anatomie", "Zyklus", "Ernährung", "Stress", "Umweltgifte", "Tees", "Tipps", "Zusammenfassung"]
|
| 24 |
+
|
| 25 |
+
# Funktion zur Erkennung der Benutzerintention
|
| 26 |
+
def erkenne_intention(text):
|
| 27 |
+
inputs = intent_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
outputs = intent_model(**inputs)
|
| 29 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 30 |
+
intent_idx = torch.argmax(probs, dim=1).item()
|
| 31 |
+
return intents[intent_idx]
|
| 32 |
+
|
| 33 |
+
# PDF-Dokument laden und verarbeiten
|
| 34 |
pdf_path = "Einsteiger Guide Leben im Einklang mit dem weiblichen Zyklus.pdf"
|
| 35 |
pdf_reader = PyPDF2.PdfReader(pdf_path)
|
| 36 |
pdf_text = ""
|
|
|
|
| 38 |
page = pdf_reader.pages[page_num]
|
| 39 |
pdf_text += page.extract_text()
|
| 40 |
|
| 41 |
+
# Define the CSS style for the UI with a mobile-friendly design and centered content
|
| 42 |
st.markdown("""
|
| 43 |
<style>
|
| 44 |
body {
|
| 45 |
font-family: 'Satoshi', sans-serif;
|
| 46 |
background-color: white;
|
| 47 |
+
display: flex;
|
| 48 |
+
justify-content: center;
|
| 49 |
+
align-items: center;
|
| 50 |
+
text-align: center;
|
| 51 |
+
padding: 0;
|
| 52 |
+
margin: 0;
|
| 53 |
}
|
| 54 |
.main {
|
| 55 |
background-color: white;
|
| 56 |
+
display: flex;
|
| 57 |
+
justify-content: center;
|
| 58 |
+
align-items: center;
|
| 59 |
+
flex-direction: column;
|
| 60 |
+
width: 100%;
|
| 61 |
+
padding: 2rem;
|
| 62 |
}
|
| 63 |
.stButton>button {
|
| 64 |
background-color: #ff4d94;
|
|
|
|
| 68 |
.stTextInput>div>div>input {
|
| 69 |
background-color: #ff99c2;
|
| 70 |
color: white;
|
| 71 |
+
width: 100%;
|
| 72 |
+
text-align: center;
|
| 73 |
}
|
| 74 |
.stTextArea>div>textarea {
|
| 75 |
background-color: #ff99c2;
|
| 76 |
color: white;
|
| 77 |
+
width: 100%;
|
| 78 |
+
text-align: center;
|
| 79 |
}
|
| 80 |
.stSubheader, .stText, .stHeader, h1, h2 {
|
| 81 |
color: #b30047;
|
|
|
|
| 102 |
""", unsafe_allow_html=True)
|
| 103 |
|
| 104 |
# Set title and subtitle
|
| 105 |
+
st.title("Willkommen bei Ihrem Menstruationszyklus-Assistenten")
|
| 106 |
+
st.subheader("Ein Assistent für Business Frauen")
|
| 107 |
|
| 108 |
+
# Eingabefelder für Benutzerparameter
|
| 109 |
+
antworten = st.text_area("Beschreiben Sie Ihr Anliegen oder Ihre Fragen (z.B. Anatomie, Zyklus, Ernährung, Stress, etc.)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Submit button
|
| 112 |
+
if st.button("Frage beantworten"):
|
| 113 |
+
if antworten:
|
| 114 |
+
# Bestimme die Intention des Benutzers
|
| 115 |
+
intent = erkenne_intention(antworten)
|
| 116 |
+
|
| 117 |
+
# Bestimme das richtige Kapitel basierend auf der Benutzerintention
|
| 118 |
+
kapitel = intent # Direktes Mapping von Intention zu Kapitel
|
| 119 |
+
|
| 120 |
+
# Wählen Sie den richtigen Agenten aus
|
| 121 |
+
agent = kapitel_agenten[kapitel]
|
| 122 |
+
|
| 123 |
+
# Generiere die Antwort mit dem ausgewählten Agenten
|
| 124 |
+
input_text = f"Frage: {antworten}\nAntwort:"
|
| 125 |
+
antwort = agent(input_text, max_new_tokens=150, num_return_sequences=1)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Display the answer
|
| 128 |
+
st.subheader(f"Antwort vom Experten für {kapitel}")
|
| 129 |
+
st.write(antwort)
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# Zeige die relevanten Informationen aus dem PDF-Dokument an
|
| 132 |
+
st.subheader("Menstrual Cycle Knowledge Base")
|
| 133 |
+
st.write(pdf_text)
|
| 134 |
else:
|
| 135 |
+
st.error("Bitte stellen Sie eine Frage.")
|
| 136 |
|
| 137 |
+
st.header("Referenzen")
|
| 138 |
st.write("""
|
| 139 |
+
Dies ist eine Beta-Version vom 2024-06-02 und wir freuen uns, Ihnen mit der aktualisierten Version zu helfen. Grüße, Nur M.
|
|
|
|
| 140 |
""")
|