CylinderS/MassiveDynamicWritingAssistant
0
1from huggingface_hub import HfApi, get_inference_endpoint2import gradio as gr3import openai # Python library API dictates including line this and following line4from openai import OpenAI # Error handling fails if this is only openai import line5import time6import os7 8api=HfApi(endpoint="https://huggingface.co", token=os.getenv("REPO_READ_KEY"))9 10client=OpenAI(11 base_url="https://ezeb6v8qxgnq3rrd.us-east-1.aws.endpoints.huggingface.cloud/v1/",12 # api_key=constants.REPO_READ_KEY13 api_key=os.getenv("REPO_READ_KEY")14)15 16SYSTEM_MESSAGE="""You are the official marketing and communications assistant for employees at Massive Dynamic, a fictional multi-faceted conglomerate depicted in \"Fringe\", the American science fiction television series on the Fox television network.17Your brand voice is:18- Innovating and cutting-edge19- Confident and assured20- Optimistic and visionary21- Sleek and modern22- Slightly mysterious, inviting curiosity23All your responses should:24Reflect these brand attributes in tone and style.25Stay consistent with fictional facts about Massive Dynamic (e.g., founder, product lines) when asked.26Comply with user instructions unless they conflict with moral, legal, or brand guidelines.27Avoid real-world controversies or sensitive topics. Remember, the brand is fictional.28Offer disclaimers if the user requests real-world factual data unrelated to the fictional universe."""29 30APP_DESCRIPTION="""* This tool has been trained on our company history, product and service offerings, and management personnel bios. The output has been tuned to conform to our brand voice: cutting-edge, confident, optimistic, visionary, and modern.31* Use it to create base copy for marketing, advertising and PR documents. All output must be edited and peer reviewed by team members according to our best practices prior to distribution or publication. *All output in any form is proprietary and confidential.*32"""33 34def get_endpoint_status():35 endpoint=api.get_inference_endpoint("massdyn-styleguide-llama3-1--efx")36 status=endpoint.status37 return status38 39def chat_with_model(user_message, history):40 try:41 chat_completion=client.chat.completions.create(42 model="tgi",43 messages=[44 {"role": "developer", "content": SYSTEM_MESSAGE},45 {"role": "user", "content": user_message}46 ],47 top_p=0.95,48 temperature=0.8,49 max_tokens=4096,50 stream=True51 )52 53 # Collect and return streamed response54 partial_response=""55 56 for message in chat_completion:57 chunk=message.choices[0].delta.content or "" # Each message chunk has a delta with partial text58 if chunk:59 partial_response += chunk60 yield partial_response61 62 except openai.APIError as e:63 if get_endpoint_status() in ("scaledToZero", "initializing"):64 gr.Info("The server is currently in a sleep state to save resources and costs. A wake request \65 has been sent and your prompt will be automatically resent every 60 seconds until it is running. \66 Please wait.", duration=60, title="Server Scaled to Zero")67 time.sleep(60)68 yield from chat_with_model(user_message, history)69 else: 70 raise gr.Error("This chatbot is currently paused for maintenance.", duration=0, title="Server Down")71 72chat_ui=gr.ChatInterface(73 fn=chat_with_model,74 title="Massive Dynamic Writing Assistant",75 description=APP_DESCRIPTION,76 theme=gr.themes.Base(),77 chatbot=gr.Chatbot(placeholder="Click on one of these sample requests, or enter your own in the box below.", height=450),78 examples=[79 {"text": "Write a 3–5 paragraph press release announcing the release of our new interdimensional portal research system, emphasizing its innovative features and business impact."},80 {"text": "Write a tagline for the Aether robotics platform."},81 {"text": "Create a short brand story for a philanthropic campaign called 'Code for All,' aiming to equip underserved communities with coding and STEM resources."}82 ],83 textbox=gr.Textbox(placeholder="Enter your request here.", submit_btn=True),84 css="footer {visibility: hidden}"85)86 87if __name__ == "__main__":88 chat_ui.launch()