memn commited on
Commit
d134796
·
verified ·
1 Parent(s): 95f19e3

Update app.py

Browse files

agenten eingeführt

Files changed (1) hide show
  1. app.py +73 -63
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
- # Initialize the Hugging Face text generation pipeline
7
- model = pipeline('text-generation', model='distilgpt2', framework='pt')
 
 
 
 
 
 
 
 
 
8
 
9
- # Load and parse the PDF file for the knowledge base
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("Welcome to Your Menstrual Cycle Tracker")
66
- st.subheader("Align with Your Body")
67
 
68
- # Input fields for user parameters
69
- days_menstruation = st.number_input("Days of Menstruation", min_value=1, max_value=10, step=1)
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("Tell Me About My Cycle"):
91
- if days_menstruation and days_cycle and last_period_start_date:
92
- # Determine current cycle phase
93
- current_phase = determine_cycle_phase(days_menstruation, days_cycle, last_period_start_date)
94
-
95
- # Fetch relevant information from the PDF
96
- phase_info = ""
97
- if "Menstruationsphase" in pdf_text and "Ernährung" in pdf_text:
98
- phase_info = pdf_text.split("Menstruationsphase")[1].split("Ernährung")[0]
99
- else:
100
- phase_info = "I am sorry I am only an Assistant and I advise you to call an expert."
101
-
102
- # Display current phase information
103
- st.subheader(f"Current Phase: {current_phase}")
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
- **Tips for Managing Different Phases:**
116
- - **Menstrual Phase:** Rest and engage in gentle activities like reading or journaling.
117
- - **Follicular Phase:** Plan new projects and gradually increase your activity.
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
- Remember, your body and cycle are unique. Embrace and understand your cycle to align better with your body's natural rhythms.
122
- """)
 
123
  else:
124
- st.error("Please fill in all the fields.")
125
 
126
- st.header("References")
127
  st.write("""
128
- This is a beta-version of 2024-06-02 and we are happy to assist you with the updated version.
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
  """)