File size: 3,238 Bytes
758f8d6
ccca2b3
758f8d6
ccca2b3
 
 
758f8d6
ccca2b3
 
 
 
 
 
 
 
 
075b08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccca2b3
075b08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccca2b3
 
 
 
075b08e
ccca2b3
 
 
075b08e
 
ccca2b3
075b08e
 
 
 
 
ccca2b3
075b08e
 
ccca2b3
075b08e
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
import gradio as gr
from huggingface_hub import InferenceClient

# HF Inference Endpoints parameter
endpoint_url = "https://rrxlgzql27e1l4x1.us-east-1.aws.endpoints.huggingface.cloud"
hf_token = "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"

# Streaming Client
client = InferenceClient(endpoint_url, token=hf_token)

# generation parameter
gen_kwargs = dict(
    max_new_tokens=512,
    temperature=0.5,
    repetition_penalty=1.02,
)

with gr.Blocks(title="Kind Words - Gradio") as demo:

    gr.Markdown(
        """
        # Kind Words - Gradio
        Please fill in the details below to create kind words
        """)
    with gr.Group():
        name_box = gr.Textbox(label="Name", value="Emily")
        situation_box = gr.Textbox(label="Situation", value="Writing a scientific blog post about using AI to protect deep-sea coral reefs")
        behavior_box = gr.Textbox(label="Behavior", value="Emily interacted professionally and efficiently, was open to making modifications based on feedback and worked proficiently with the time constraints of others")
        impact_box = gr.Textbox(label="Impact", value="A professional and engaging science blog post was created that communicated our science well")
        with gr.Row():
            perspective_dropdown = gr.Dropdown(["First Person", "Third Person"], label="Perspective", value="Third Person")
            length_dropdown = gr.Dropdown(["Short", "Medium", "Long"], label="Output length", value="Medium")
            praise_dropdown = gr.Dropdown(["Minimal", "Modest", "High"], label="Praise level", value="Modest")

    submit_btn = gr.Button("Submit")

    error_box = gr.Textbox(label="Error", visible=False)

    with gr.Column() as output_col:
        output_box = gr.Textbox(label="Output")

    def submit(name, situation, behavior, impact, perspective, length, praise):
        if len(name) == 0:
            return {error_box: gr.update(value="Enter name", visible=True)}
        elif len(situation) == 0:
            return {error_box: gr.update(value="Enter a description of the situation", visible=True)}
        elif len(behavior) == 0:
            return {error_box: gr.update(value="Enter a description of the behavior", visible=True)}
        elif len(impact) == 0:
            return {error_box: gr.update(value="Enter a description of the impact", visible=True)}

        if length == "Short":
            output_length = "a short paragraph"
        elif length == "Long":
            output_length = "several paragraphs"
        else:
            output_length = "a couple of paragraphs"

        if praise == "High":
            praise = "lots of"

        prompt = f"""The situation was: {situation}. The behavior was: {behavior}. The impact was: {impact}. Write {output_length} in a {perspective.lower()} perspective about {name}, providing positive feedback with {praise.lower()} praise."""

        response = client.text_generation(prompt, stream=False, details=False, **gen_kwargs)

        return {
            output_box: response.strip(),
        }

    submit_btn.click(
        submit,
        [name_box, situation_box, behavior_box, impact_box, perspective_dropdown, length_dropdown, praise_dropdown],
        output_box,
    )


demo.launch()