Spaces:
Sleeping
Sleeping
File size: 4,581 Bytes
7d3fe61 501fdf4 daaec6b 501fdf4 99b19b0 501fdf4 61f0de7 7d3fe61 501fdf4 61f0de7 7d3fe61 e36fac3 7d3fe61 61f0de7 0e5b461 7d3fe61 92a04df 36f4882 7d3fe61 36f4882 7d3fe61 36f4882 7d3fe61 36f4882 7d3fe61 36f4882 7d3fe61 61f0de7 71e4af4 270b57a 71e4af4 270b57a 71e4af4 270b57a 92a04df 61f0de7 36f4882 7d3fe61 2027b4c 7d3fe61 92a04df 7d3fe61 23dc704 7d3fe61 1582914 61f0de7 0e5b461 7d3fe61 e36fac3 7d3fe61 61f0de7 6669a57 71e4af4 61f0de7 81ab8fb 85b80e5 81ab8fb 10b1cf3 7d3fe61 61f0de7 125c7b2 61f0de7 125c7b2 61f0de7 7d3fe61 71e4af4 61f0de7 7d3fe61 71e4af4 61f0de7 92a04df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#importing libraries
import gradio as gr
import tensorflow.keras as keras
import time
import keras_nlp
import os
model_path = "Zul001/HydroSense_Gemma_Finetuned_Model"
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(f"hf://{model_path}")
# reset_triggered = False
custom_css = """
@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');
.gradio-container, .gradio-container * {
font-family: "Playfair Display", serif;
font-optical-sizing: auto;
font-weight: <weight>;
font-style: normal;
}
"""
js = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') === 'light') {
url.searchParams.set('__theme', 'light');
window.location.href = url.href;
}
}
"""
previous_sessions = []
def post_process_output(prompt, result):
# Remove the prompt if it's repeated at the beginning of the answer
answer = result.strip()
if answer.startswith(prompt):
answer = answer[len(prompt):].strip()
# Remove any leading colons or whitespace
answer = answer.lstrip(':')
# Ensure the answer starts with a capital letter
answer = answer.capitalize()
# Ensure the answer ends with a period if it doesn't already
if not answer.endswith('.'):
answer += '.'
return f"{answer}"
def add_session(prompt):
global previous_sessions
session_name = ' '.join(prompt.split()[:5])
if session_name and session_name not in previous_sessions:
previous_sessions.append(session_name)
return "\n".join(previous_sessions) # Return only the session logs as a string
def inference(prompt):
prompt_text = prompt
generated_text = gemma_lm.generate(prompt_text)
#Apply post-processing
formatted_output = post_process_output(prompt_text, generated_text)
print(formatted_output)
#adding a bit of delay
time.sleep(1)
result = formatted_output
sessions = add_session(prompt_text)
return result, sessions
# def inference(prompt):
# time.sleep(1)
# result = "Your Result"
# # sessions = add_session(prompt)
# return result
# def remember(prompt, result):
# global memory
# # Store the session as a dictionary
# session = {'prompt': prompt, 'result': result}
# memory.append(session)
# # Update previous_sessions for display
# session_display = [f"Q: {s['prompt']} \nA: {s['result']}" for s in memory]
# return "\n\n".join(session_display) # Return formatted sessions as a string
def clear_sessions():
global previous_sessions
previous_sessions.clear()
return "\n".join(previous_sessions)
def clear_fields():
global reset_triggered
# reset_triggered = True
return "", "" # Return empty strings to clear the prompt and output fields
with gr.Blocks(theme='gradio/soft', css=custom_css) as demo:
gr.Markdown("<center><h1>HydroSense LLM Demo</h1></center>")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Previous Sessions")
session_list = gr.Textbox(label="Sessions", value="\n".join(previous_sessions), interactive=False, lines=4, max_lines=20)
add_button = gr.Button("New Session")
clear_session = gr.Button("Clear Session")
with gr.Column(scale=2):
output = gr.Textbox(label="Result", lines=5, max_lines=20)
prompt = gr.Textbox(label="Enter your Prompt here", max_lines=20)
with gr.Row():
generate_btn = gr.Button("Generate Answer", variant="primary", size="sm")
reset_btn = gr.Button("Clear Content", variant="secondary", size="sm", elem_id="primary")
generate_btn.click(
fn=inference,
inputs=[prompt],
outputs=[output, session_list]
)
prompt.submit(
fn=inference,
inputs=[prompt],
outputs=[output, session_list],
)
reset_btn.click(
lambda: ("", ""),
inputs=None,
outputs=[prompt, output]
)
# Button to clear the prompt and output fields
add_button.click(
fn=clear_fields, # Only call the clear_fields function
inputs=None, # No inputs needed
outputs=[prompt, output] # Clear the prompt and output fields
)
clear_session.click(
fn=clear_sessions,
inputs=None,
outputs=[session_list]
)
demo.launch(share=True) |