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()