File size: 2,317 Bytes
9bd7774
dca9cd6
9bd7774
 
 
b6c6daf
24a8b37
 
 
 
 
 
 
 
9bd7774
 
9b8838a
9bd7774
9b8838a
9bd7774
 
 
 
 
fd0e78f
 
9bd7774
fd0e78f
 
 
9bd7774
 
 
 
 
 
fd0e78f
9bd7774
b6c6daf
9bd7774
 
 
 
 
 
 
 
 
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
from huggingface_hub import InferenceClient
import gradio as gr

client = InferenceClient("grammarly/coedit-large")

def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, top_k=50, repetition_penalty=1.0):
    print(f"  TEMP: {temperature}     \n\t TYPE: {type(temperature)}")
    print(f"  TOP-P: {top_p}     \n\t TYPE: {type(top_p)}")
    print(f"  TOP-K: {top_k}     \n\t TYPE: {type(top_k)}")
    print(f"  MAX_TOK: {max_new_tokens}     \n\t TYPE: {type(max_new_tokens)}")
    
    
    #temperature = float(temperature)
    temperature = float(temperature[0]) if isinstance(temperature, list) else float(temperature)
    if temperature < 1e-2: temperature = 1e-2
    top_p = float(top_p)
    top_k = int(top_k)  # Ensure top_k is an integer, as it was being treated like a float

    generate_kwargs = dict(temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty)  # seed=42,)

    formatted_prompt = "Fix grammatical errors in this sentence: " + prompt
    print("\nPROMPT: \n\t" + formatted_prompt)

    # Generate text from the HF inference
    output = client.text_generation(formatted_prompt, **generate_kwargs, details=True, return_full_text=True)
    #output = ""

    #for response in stream:
    #    output += response.token.text
    #    yield output
    return output
    


additional_inputs=[
    gr.Slider( label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs", ),
    gr.Slider( label="Max new tokens", value=150, minimum=0, maximum=250, step=64, interactive=True, info="The maximum numbers of new tokens", ),
    gr.Slider( label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens", ),
    gr.Slider( label="Top-k", value=50, minimum=0, maximum=100, step=1, interactive=True, info="Limits the number of top-k tokens considered at each step"),
]

gr.ChatInterface(
    fn=generate,
    chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
    additional_inputs=additional_inputs,
    title="My Grammarly Space",
    concurrency_limit=20,
).launch(show_api=False)