PascalZhan commited on
Commit
e5f6861
1 Parent(s): 40b03a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -88
app.py CHANGED
@@ -1,88 +1,89 @@
1
- # Author: Bastien & Pascal
2
- # Date: 2/25/2024
3
- # Project: SAE-GPT2 | BUT 3 Informatique - Semester 5
4
-
5
- # Import of required libraries
6
- import os
7
-
8
- os.system("pip install --upgrade pip")
9
- os.system("pip install googletrans-py")
10
- os.system("pip install tensorflow==2.15.0")
11
- os.system("pip install keras-nlp")
12
- os.system("pip install -q --upgrade keras") # Upgrade Keras to version 3
13
-
14
- import time
15
- import keras
16
- import keras_nlp
17
- import pandas as pd
18
- import gradio as gr
19
- from googletrans import Translator
20
- from importHuggingFaceHubModel import from_pretrained_keras
21
-
22
- # Set Keras Backend to Tensorflow
23
- os.environ["KERAS_BACKEND"] = "tensorflow"
24
-
25
- # Load the fine-tuned model
26
- #model = keras.models.load_model("LoRA_Model_V2.keras")
27
- model = from_pretrained_keras('DracolIA/GPT-2-LoRA-HealthCare')
28
-
29
- translator = Translator() # Create Translator Instance
30
-
31
- # Function to generate responses from the model
32
- def generate_responses(question):
33
- language = translator.detect(question).lang.upper() # Verify the language of the prompt
34
- if language != "EN":
35
- question = translator.translate(question, src=language, dest="en").text # Translation of user text to english for the model
36
-
37
- prompt = f"[QUESTION] {question} [ANSWER]"
38
- # Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
39
- output = clean_answer_text(model.generate(prompt, max_length=1024))
40
-
41
- # Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
42
- if language != "EN":
43
- output = Translator().translate(output, src="en", dest=language).text # Translation of model's text to user's language
44
-
45
- return output
46
-
47
- # Function clean the output of the model from the prompt engineering done in the "generate_responses" function
48
- def clean_answer_text(text: str) -> str:
49
- # Define the start marker for the model's response
50
- response_start = text.find("[ANSWER]") + len("[ANSWER]")
51
-
52
- # Extract everything after "Doctor:"
53
- response_text = text[response_start:].strip()
54
- last_dot_index = response_text.rfind(".")
55
- if last_dot_index != -1:
56
- response_text = response_text[:last_dot_index + 1]
57
-
58
- # Additional cleaning if necessary (e.g., removing leading/trailing spaces or new lines)
59
- response_text = response_text.strip()
60
-
61
- return response_text
62
-
63
-
64
- # Define a Gradio interface
65
- def chat_interface(question, history_df):
66
- response = generate_responses(question)
67
- # Insert the new question and response at the beginning of the DataFrame
68
- history_df = pd.concat([pd.DataFrame({"Question": [question], "Réponse": [response]}), history_df], ignore_index=True)
69
- return response, history_df
70
-
71
- with gr.Blocks() as demo:
72
- gr.HTML("""
73
- <div style='width: 100%; height: 200px; background: url("https://github.com/BastienHot/SAE-GPT2/raw/70fb88500a2cc168d71e8ed635fc54492beb6241/image/logo.png") no-repeat center center; background-size: contain;'>
74
- <h1 style='text-align:center; width=100%'>DracolIA - AI Question Answering for Healthcare</h1>
75
- </div>
76
- """)
77
- with gr.Row():
78
- question = gr.Textbox(label="Votre Question", placeholder="Saisissez ici...")
79
- submit_btn = gr.Button("Envoyer")
80
- response = gr.Textbox(label="Réponse", interactive=False)
81
-
82
- # Initialize an empty DataFrame to keep track of question-answer history
83
- history_display = gr.Dataframe(headers=["Question", "Réponse"], values=[], interactive=False)
84
-
85
- submit_btn.click(fn=chat_interface, inputs=[question, history_display], outputs=[response, history_display])
86
-
87
- if __name__ == "__main__":
88
- demo.launch()
 
 
1
+ # Author: Bastien & Pascal
2
+ # Date: 2/25/2024
3
+ # Project: SAE-GPT2 | BUT 3 Informatique - Semester 5
4
+
5
+ # Import of required libraries
6
+ import os
7
+
8
+ os.system("pip install --upgrade pip")
9
+ os.system("pip install googletrans-py")
10
+ os.system("pip install tensorflow==2.15.0")
11
+ os.system("pip install keras-nlp")
12
+ os.system("pip install -q --upgrade keras") # Upgrade Keras to version 3
13
+
14
+ import time
15
+ import keras
16
+ import keras_nlp
17
+ import pandas as pd
18
+ import gradio as gr
19
+ from googletrans import Translator
20
+ #from importHuggingFaceHubModel import from_pretrained_keras
21
+ from huggingface_hub import from_pretrained_keras
22
+
23
+ # Set Keras Backend to Tensorflow
24
+ os.environ["KERAS_BACKEND"] = "tensorflow"
25
+
26
+ # Load the fine-tuned model
27
+ #model = keras.models.load_model("LoRA_Model_V2.keras")
28
+ model = from_pretrained_keras('DracolIA/GPT-2-LoRA-HealthCare')
29
+
30
+ translator = Translator() # Create Translator Instance
31
+
32
+ # Function to generate responses from the model
33
+ def generate_responses(question):
34
+ language = translator.detect(question).lang.upper() # Verify the language of the prompt
35
+ if language != "EN":
36
+ question = translator.translate(question, src=language, dest="en").text # Translation of user text to english for the model
37
+
38
+ prompt = f"[QUESTION] {question} [ANSWER]"
39
+ # Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
40
+ output = clean_answer_text(model.generate(prompt, max_length=1024))
41
+
42
+ # Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
43
+ if language != "EN":
44
+ output = Translator().translate(output, src="en", dest=language).text # Translation of model's text to user's language
45
+
46
+ return output
47
+
48
+ # Function clean the output of the model from the prompt engineering done in the "generate_responses" function
49
+ def clean_answer_text(text: str) -> str:
50
+ # Define the start marker for the model's response
51
+ response_start = text.find("[ANSWER]") + len("[ANSWER]")
52
+
53
+ # Extract everything after "Doctor:"
54
+ response_text = text[response_start:].strip()
55
+ last_dot_index = response_text.rfind(".")
56
+ if last_dot_index != -1:
57
+ response_text = response_text[:last_dot_index + 1]
58
+
59
+ # Additional cleaning if necessary (e.g., removing leading/trailing spaces or new lines)
60
+ response_text = response_text.strip()
61
+
62
+ return response_text
63
+
64
+
65
+ # Define a Gradio interface
66
+ def chat_interface(question, history_df):
67
+ response = generate_responses(question)
68
+ # Insert the new question and response at the beginning of the DataFrame
69
+ history_df = pd.concat([pd.DataFrame({"Question": [question], "Réponse": [response]}), history_df], ignore_index=True)
70
+ return response, history_df
71
+
72
+ with gr.Blocks() as demo:
73
+ gr.HTML("""
74
+ <div style='width: 100%; height: 200px; background: url("https://github.com/BastienHot/SAE-GPT2/raw/70fb88500a2cc168d71e8ed635fc54492beb6241/image/logo.png") no-repeat center center; background-size: contain;'>
75
+ <h1 style='text-align:center; width=100%'>DracolIA - AI Question Answering for Healthcare</h1>
76
+ </div>
77
+ """)
78
+ with gr.Row():
79
+ question = gr.Textbox(label="Votre Question", placeholder="Saisissez ici...")
80
+ submit_btn = gr.Button("Envoyer")
81
+ response = gr.Textbox(label="Réponse", interactive=False)
82
+
83
+ # Initialize an empty DataFrame to keep track of question-answer history
84
+ history_display = gr.Dataframe(headers=["Question", "Réponse"], values=[], interactive=False)
85
+
86
+ submit_btn.click(fn=chat_interface, inputs=[question, history_display], outputs=[response, history_display])
87
+
88
+ if __name__ == "__main__":
89
+ demo.launch()