asunsada commited on
Commit
012f977
·
verified ·
1 Parent(s): ee63d71

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import gradio as gr
4
+ import keras_hub
5
+ import os
6
+
7
+ import re
8
+
9
+
10
+
11
+
12
+ model_path="kaggle://asunsada/gemma2_2b_it_en_roleplay/keras/football_coach_11042024_epoch15" # kaggle
13
+
14
+
15
+ class GemmaChatbot:
16
+ def __init__(self):
17
+ # Initialize the model
18
+ #preset = "gemma_instruct_2b_en" # name of pretrained Gemma 2
19
+ #self.gemma_llm = keras_hub.models.GemmaCausalLM.from_preset(preset)
20
+ # Load your custom model
21
+ self.gemma_llm = keras_hub.models.GemmaCausalLM.from_preset(model_path)
22
+ print(self.gemma_llm)
23
+
24
+ def format_prompt(self, message, history):
25
+ # Format conversation history into a single string
26
+ formatted_history = ""
27
+ for user_msg, assistant_msg in history:
28
+ formatted_history += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
29
+
30
+ # Add the current message
31
+ prompt = formatted_history + f"User: {message}\nAssistant:"
32
+ return prompt
33
+
34
+ def generate_response(self, message, history):
35
+ # Format the prompt with history
36
+
37
+ #prompt = self.format_prompt(message, history)
38
+ prompt= message
39
+ # Generate response
40
+ output = self.gemma_llm.generate(prompt,256)
41
+ output = clean_incomplete_sentences(output) # remove incomplete sentences (typically
42
+ # last one or any question at the end.
43
+ return output.replace(prompt, "").strip()
44
+
45
+ # remove incomplete sentences (typically last one or any question at the end.)
46
+ def clean_incomplete_sentences(text):
47
+ # Split text into sentences using regular expressions
48
+ sentences = re.split(r'(?<=\.) |(?<=\?) |(?<=!) ', text)
49
+
50
+ # Filter out incomplete sentences (those not ending with ".", "?" or "!")
51
+ complete_sentences = [s for s in sentences if re.search(r'[.!?]$', s)]
52
+
53
+ # Remove the last sentence if it ends with a question mark
54
+ if complete_sentences and complete_sentences[-1].endswith('?'):
55
+ complete_sentences = complete_sentences[:-1]
56
+
57
+ # Join sentences back into a single string
58
+ cleaned_text = ' '.join(complete_sentences)
59
+ return cleaned_text
60
+
61
+ def create_chatbot():
62
+ # Initialize the chatbot
63
+ chatbot = GemmaChatbot()
64
+
65
+ # Create the Gradio interface
66
+ chat_interface = gr.ChatInterface(
67
+ fn=chatbot.generate_response,
68
+ title="🏈 Tackle Tutor Chatbot 🏈 💬",
69
+ description="I'm Tackle Tutor, the head coach of the greatest football team around! "
70
+ "With over 20 years of coaching experience and numerous championships under my belt, "
71
+ "I've also had the honor of coaching and playing in the NFL. \n\n"
72
+ "Ask me how to tackle any challenge, on the field or in life, and I'll guide you through it",
73
+ examples=[
74
+ "Why is resilience important in football and life?",
75
+ "How can I keep trying when something is really hard?",
76
+ "How do famous coaches inspire?",
77
+ "What can I learn from coach Barry Switzer?",
78
+ "How can football teach us about teamwork?",
79
+ "Why is preparation essential for success?",
80
+ "Why should we always give our best effort?",
81
+ ],
82
+ theme=gr.themes.Soft()
83
+ )
84
+
85
+ return chat_interface
86
+
87
+
88
+ # Launch the chatbot
89
+ if __name__ == "__main__":
90
+ # Create and launch the interface
91
+ chat_interface = create_chatbot()
92
+ chat_interface.launch(share=True)