Spaces:
Sleeping
Sleeping
Create app.py file
Browse files
app.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install keras_nlp kaggle gradio tensorflow
|
2 |
+
!pip install huggingface_hub
|
3 |
+
|
4 |
+
#importing libraries
|
5 |
+
import keras_nlp
|
6 |
+
import os
|
7 |
+
import tensorflow as tf
|
8 |
+
import kagglehub
|
9 |
+
|
10 |
+
|
11 |
+
#Loading the model from kaggle
|
12 |
+
#For safety, add this to kaggle or google secrets
|
13 |
+
os.environ['KAGGLE_USERNAME'] = 'ayomidezulkazeem' #username
|
14 |
+
os.environ['KAGGLE_KEY'] = '245a313b018fe8355198d9c48413b991' #kaggle_key
|
15 |
+
|
16 |
+
|
17 |
+
# Download latest version
|
18 |
+
path = kagglehub.model_download("ayomidezulkazeem/hydrosense/keras/gemma-hydrosense-instruct-2b")
|
19 |
+
|
20 |
+
print("Path to model files:", path)
|
21 |
+
print("Model path:", path)
|
22 |
+
|
23 |
+
|
24 |
+
model_path = path
|
25 |
+
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(model_path)
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
import gradio as gr
|
30 |
+
import time
|
31 |
+
from huggingface_hub import InferenceClient
|
32 |
+
import os
|
33 |
+
|
34 |
+
custom_css = """
|
35 |
+
@import url('https://fonts.googleapis.com/css2?family=Edu+AU+VIC+WA+NT+Dots:[email protected]&family=Give+You+Glory&family=Sofia&family=Sunshiney&family=Vujahday+Script&display=swap');
|
36 |
+
.gradio-container, .gradio-container * {
|
37 |
+
font-family: "Playfair Display", serif;
|
38 |
+
font-optical-sizing: auto;
|
39 |
+
font-weight: <weight>;
|
40 |
+
font-style: normal;
|
41 |
+
}
|
42 |
+
"""
|
43 |
+
js = """
|
44 |
+
function refresh() {
|
45 |
+
const url = new URL(window.location);
|
46 |
+
if (url.searchParams.get('__theme') === 'light') {
|
47 |
+
url.searchParams.set('__theme', 'light');
|
48 |
+
window.location.href = url.href;
|
49 |
+
}
|
50 |
+
}
|
51 |
+
"""
|
52 |
+
|
53 |
+
|
54 |
+
previous_sessions = []
|
55 |
+
memory = [{}]
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
def inference(prompt_text):
|
60 |
+
prompt_text = prompt_text
|
61 |
+
generated_text = gemma_lm.generate(prompt_text)
|
62 |
+
|
63 |
+
#Apply post-processing
|
64 |
+
formatted_output = post_process_output(prompt_text, generated_text)
|
65 |
+
print(formatted_output)
|
66 |
+
|
67 |
+
#adding a bit of delay
|
68 |
+
time.sleep(1)
|
69 |
+
result = formatted_output
|
70 |
+
sessions = add_session(prompt_text)
|
71 |
+
return result, sessions
|
72 |
+
|
73 |
+
|
74 |
+
def remember(prompt, result):
|
75 |
+
global memory
|
76 |
+
# Store the session as a dictionary
|
77 |
+
session = {'prompt': prompt, 'result': result}
|
78 |
+
memory.append(session)
|
79 |
+
|
80 |
+
# Update previous_sessions for display
|
81 |
+
session_display = [f"Q: {s['prompt']} \nA: {s['result']}" for s in memory]
|
82 |
+
|
83 |
+
return "\n\n".join(session_display) # Return formatted sessions as a string
|
84 |
+
|
85 |
+
|
86 |
+
def add_session(prompt_text):
|
87 |
+
global previous_sessions
|
88 |
+
session_name = ' '.join(prompt_text.split()[:5])
|
89 |
+
|
90 |
+
if session_name and session_name not in previous_sessions:
|
91 |
+
previous_sessions.append(session_name)
|
92 |
+
|
93 |
+
return "\n".join(previous_sessions) # Return only the session logs as a string
|
94 |
+
|
95 |
+
|
96 |
+
def clear_sessions():
|
97 |
+
global previous_sessions
|
98 |
+
previous_sessions.clear()
|
99 |
+
return "\n".join(previous_sessions)
|
100 |
+
|
101 |
+
def clear_fields():
|
102 |
+
return "", "" # Return empty strings to clear the prompt and output fields
|
103 |
+
|
104 |
+
|
105 |
+
with gr.Blocks(theme='gradio/soft', css=custom_css) as demo:
|
106 |
+
gr.Markdown("<center><h1>HydroFlow LLM Demo</h1></center>")
|
107 |
+
gr.Markdown("<center><h3><i><em>Ask me anything on Wastewater or Stormwater!</em></i></h3></center>")
|
108 |
+
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Column(scale=1):
|
111 |
+
gr.Markdown("## Previous Sessions")
|
112 |
+
session_list = gr.Textbox(label="Sessions", value="\n".join(previous_sessions), interactive=False, lines=4, max_lines=20)
|
113 |
+
add_button = gr.Button("New Session")
|
114 |
+
clear_session = gr.Button("Clear Session")
|
115 |
+
|
116 |
+
with gr.Column(scale=2):
|
117 |
+
output = gr.Textbox(label="Result", lines=5, max_lines=20)
|
118 |
+
prompt = gr.Textbox(label="Enter your Prompt here", max_lines=20)
|
119 |
+
|
120 |
+
with gr.Row():
|
121 |
+
generate_btn = gr.Button("Generate Answer", variant="primary", size="sm")
|
122 |
+
reset_btn = gr.Button("Clear Content", variant="secondary", size="sm", elem_id="primary")
|
123 |
+
|
124 |
+
|
125 |
+
generate_btn.click(
|
126 |
+
fn=inference,
|
127 |
+
inputs=[prompt],
|
128 |
+
outputs=[output, session_list],
|
129 |
+
)
|
130 |
+
|
131 |
+
prompt.submit(
|
132 |
+
fn=inference,
|
133 |
+
inputs=[prompt],
|
134 |
+
outputs=[output, session_list],
|
135 |
+
)
|
136 |
+
|
137 |
+
reset_btn.click(
|
138 |
+
lambda: ("", ""),
|
139 |
+
inputs=None,
|
140 |
+
outputs=[prompt, output]
|
141 |
+
)
|
142 |
+
|
143 |
+
|
144 |
+
# Button to clear the prompt and output fields
|
145 |
+
add_button.click(
|
146 |
+
fn=clear_fields, # Only call the clear_fields function
|
147 |
+
inputs=None, # No inputs needed
|
148 |
+
outputs=[prompt, output] # Clear the prompt and output fields
|
149 |
+
)
|
150 |
+
|
151 |
+
|
152 |
+
clear_session.click(
|
153 |
+
fn=clear_sessions,
|
154 |
+
inputs=None,
|
155 |
+
outputs=[session_list]
|
156 |
+
)
|
157 |
+
|
158 |
+
demo.launch(share=True)
|