File size: 4,468 Bytes
501fdf4
 
daaec6b
501fdf4
99b19b0
501fdf4
61f0de7
 
501fdf4
 
61f0de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36f4882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61f0de7
e29631c
 
 
270b57a
e29631c
 
270b57a
e29631c
270b57a
61f0de7
 
36f4882
 
 
61f0de7
36f4882
 
 
61f0de7
36f4882
 
 
 
 
61f0de7
cc47b41
36f4882
a2a6500
36f4882
 
 
 
cc47b41
 
f9dd6fb
 
 
 
 
 
 
 
61f0de7
f9dd6fb
61f0de7
 
270b57a
e29631c
 
 
 
61f0de7
 
 
 
 
 
 
6669a57
61f0de7
f9dd6fb
 
61f0de7
e4abd98
61f0de7
 
 
 
 
10b1cf3
 
 
 
 
 
 
 
 
61f0de7
 
 
 
752c16d
61f0de7
 
 
 
 
752c16d
61f0de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6a0afb
 
 
 
 
61f0de7
 
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}")




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


# 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():
    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>HydroFlow LLM Demo</h1></center>")
    

    with gr.Row():
        with gr.Column(scale=1):
            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")

    
        # 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")

        


    generate_btn.click(
        fn=inference,
        inputs=[prompt],
        outputs=[output]
    )

    prompt.submit(
        fn=inference,
        inputs=[prompt],
        outputs=[output],
    )

    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)