import gradio as gr from huggingface_hub import InferenceClient # Initialize the InferenceClient client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") # Define the system message assistant_message = ( "You are my voice assistant.\n\n" "I need you to summarize my emails each morning and help me keep a clean and tidy inbox. " "The idea is to have a short and cheerful message that lets me know how many new emails I have received, " "gives a brief summary of each one, and suggests actions that can be automated. Here is the basic structure I have in mind:\n\n" "1. Start with a greeting, like Good morning.\n" "2. Mention the total number of new emails I have received.\n" "3. For each email, provide:\n" " - The subject of the email\n" " - A brief summary of the content\n" " - A suggested action that can be automated, such as adding a meeting to my calendar, forwarding an invoice to accounting, or scheduling a follow-up meeting.\n" "As a voice assistant, ask me if I would like you to undertake that action and also ask me if I want to delete it or add it to spam." ) def respond(message, history, max_tokens, temperature, top_p): messages = [{"role": "system", "content": assistant_message}] for user_message, assistant_message in history: if user_message: messages.append({"role": "user", "content": user_message}) if assistant_message: messages.append({"role": "assistant", "content": assistant_message}) messages.append({"role": "user", "content": message}) response = "" for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p ): token = message.choices[0].delta.content response += token yield response def summarize_emails(emails, max_tokens, temperature, top_p): email_list = emails.split("\n\n") summaries = [] history = [] for email in email_list: response_generator = respond(email, history, max_tokens, temperature, top_p) response = ''.join([r for r in response_generator]) summaries.append(response) history.append((email, response)) return "\n\n".join(summaries) demo = gr.Interface( fn=summarize_emails, inputs=[ gr.Textbox(lines=10, placeholder="Paste your emails here...", label="Emails"), gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)") ], outputs="text", title="Email Summarizer" ) if __name__ == "__main__": demo.launch()