File size: 7,289 Bytes
6aef632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

import gradio as gr
from llama_cpp import Llama

from huggingface_hub import hf_hub_download
import os
#https://llama-cpp-python.readthedocs.io/en/latest/api-reference/

model_path = hf_hub_download(
    repo_id="ViktorMardskog/lora_model_3e-4LR_10k_1b_val",
    filename="llama-3.2-1b-instruct.Q4_K_M.gguf",
    repo_type="model",
) 

llm = Llama(model_path=model_path, n_ctx=500,  n_gpu_layers=0, verbose=False)

#to set brainstoorming model to same, uncomment this and comment the other.
""" model_path_B = hf_hub_download(
    repo_id="ViktorMardskog/lora_model_3e-4LR_10k_1b_val",
    filename="llama-3.2-1b-instruct.Q4_K_M.gguf",
    repo_type="model",
) """


model_path_B = hf_hub_download(
    repo_id="ViktorMardskog/lora_model_3e-4LR_1b_val_BrainStoorming",
    filename="llama-3.2-1b-instruct.Q4_K_M.gguf",
    repo_type="model",
) 

llm_brainstoorming = Llama(model_path=model_path_B, n_ctx=500,  n_gpu_layers=0, verbose=False)




def evaluate(expanded_idea):
    messages = [{"role": "system", "content": "You are an assistant."}]

    message =f"Idea to be evaluated: {expanded_idea}.\n Evaluate the idea with a score (1–10) and give 3 concrete concerns.\n Format:\n Score: X/10\n Concerns:\n 1. ...\n 2. ...\n 3. ..."

    messages.append({"role": "user", "content": message})

    #This code snippet below is directly taken from the UI chatbot template in huggingface (gradio)
    response = ""

    for message_part in llm.create_chat_completion(
        messages,
        max_tokens=128,
        stream=True,
        temperature=0.8,
        top_p=0.9,
    ):
        choices = message_part['choices']
        token = ""
        if choices and choices[0]["delta"].get("content"):
            token =choices[0]["delta"]["content"]
        response += token
        yield response

def refine(expanded_idea):
    messages = [{"role": "system", "content": "You are an assistant."}]

    message =f"Refine the following idea: {expanded_idea}."

    messages.append({"role": "user", "content": message})

    #This code snippet below is directly taken from the UI chatbot template in huggingface (gradio)
    response = ""

    for message_part in llm.create_chat_completion(
        messages,
        max_tokens=128,
        stream=True,
        temperature=0.8,
        top_p=0.9,
    ):
        choices = message_part['choices']
        token = ""
        if choices and choices[0]["delta"].get("content"):
            token =choices[0]["delta"]["content"]
        response += token
        yield response

def brainstorm(expanded_idea):
    messages = [{"role": "system", "content": "You are an assistant."}]

    message =f"Idea Input: {expanded_idea}. Brainstorm three features this idea could include. Reply ONLY with bullet-points.\n"

    messages.append({"role": "user", "content": message})

    #This code snippet below is directly taken from the UI chatbot template in huggingface (gradio)
    response = ""

    for message_part in llm_brainstoorming.create_chat_completion(
        messages = messages,
        max_tokens=128,
        stream=True,
        temperature=0.8,
        top_p=0.9,
    ):
        choices = message_part['choices']
        token = ""
        if choices and choices[0]["delta"].get("content"):
            token =choices[0]["delta"]["content"]
        response += token
        yield response


def add_note(notes,title):
    if notes is None:
        notes = []

    title =title.strip()

    notes = notes +[{"title": title, "text": ""}]
    choices = [n["title"] for n in notes]

    return gr.update(choices=choices, value=title), notes,""

def select_note(notes, selected_title):
    if notes is None:
        notes = []
    if not selected_title:
        return "", notes

    for n in notes:
        if n["title"] == selected_title:
            return n["text"], notes

    return "", notes

def update_note_text(notes_state, selected_title, new_expanded_idea):
    if not selected_title:
        return notes_state
    if notes_state is None:
        notes_state = []
    
    new_notes= []
    for note in notes_state:
        if note["title"] == selected_title:
            new_notes.append({"title": note["title"], "text": new_expanded_idea})
        else:
            new_notes.append(note)
    return new_notes

def delete_note(notes, selected_title):
    if notes is None: notes = []
    if not selected_title:
        #then keep everything like before because not selected any for deletion
        choices = [n["title"] for n in notes]
        return gr.update(choices=choices,value=None), notes

    new_notes =[]
    for note in notes:
        if note["title"]!= selected_title:
            new_notes.append(note)
    
    choices = []
    for new_note in new_notes:
        choices.append(new_note["title"])

    return gr.update(choices=choices, value=None),new_notes



def append_LLM_text(expanded_idea, llm_output):
    if not llm_output: return expanded_idea
    if not expanded_idea: return llm_output

    return expanded_idea + "\n" + llm_output

def replace_idea_text(expanded_idea, llm_output):
    new =llm_output or expanded_idea
    return new

with gr.Blocks(title="Brainstorming helper") as demo:
    notes_state = gr.State([])  #title --> body

    with gr.Row():

        with gr.Column(scale=2):
            gr.Markdown("Sticky notes")
            notes_list = gr.Radio(choices=[], label="Ideas", interactive=True)
            new_title = gr.Textbox(label="New idea title", placeholder="Short title for the idea")
            add_note_btn = gr.Button("Add sticky note")
            delete_btn = gr.Button("Delete selected note")

        with gr.Column(scale=3):
            expanded_idea = gr.Textbox(label="Expanded idea", lines=12, placeholder="Describe the idea..(stickynote has to be selected)")
            with gr.Row():
                save_btn = gr.Button("Save idea")
            with gr.Row():
                refine_btn = gr.Button("Refine idea")
                brainstorm_btn = gr.Button("Brainstorm around idea")
                evaluate_btn = gr.Button("Evaluate idea")

            llm_output_box=gr.Textbox(label="LLM output",lines=10,interactive=False)
            with gr.Row():
                append_btn = gr.Button("Append to expanded idea")
                replace_btn = gr.Button("Replace expanded idea")

    save_btn.click(update_note_text, inputs=[notes_state, notes_list, expanded_idea], outputs=[notes_state])

    refine_btn.click(refine, inputs=expanded_idea, outputs=llm_output_box)
    brainstorm_btn.click(brainstorm, inputs=expanded_idea, outputs=llm_output_box)
    evaluate_btn.click(evaluate, inputs=expanded_idea, outputs=llm_output_box)

    
    add_note_btn.click(add_note, inputs=[notes_state, new_title],outputs=[notes_list, notes_state, expanded_idea])
    notes_list.change(select_note, inputs=[notes_state, notes_list],outputs=[expanded_idea, notes_state])
    delete_btn.click(delete_note, inputs=[notes_state, notes_list], outputs=[notes_list, notes_state])

    append_btn.click(append_LLM_text,inputs=[expanded_idea, llm_output_box], outputs=[expanded_idea])

    replace_btn.click(replace_idea_text, inputs=[expanded_idea, llm_output_box], outputs=[expanded_idea])


if __name__ == "__main__":
    demo.queue()
    demo.launch()