Guest User

Untitled

a guest
Oct 28th, 2026
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. from mlx_lm import load, generate
  2. import os
  3. from dotenv import load_dotenv
  4. from langchain.agents import create_agent, AgentState
  5. from langchain.messages import HumanMessage, ToolMessage, AIMessage
  6. from langchain_core.prompts import PromptTemplate
  7. from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
  8. from langchain_community.tools.playwright.utils import create_sync_playwright_browser
  9. from langchain_core.prompts import ChatPromptTemplate
  10. from langchain_ollama import ChatOllama
  11. import re
  12. from langchain_classic.agents import AgentExecutor, create_tool_calling_agent
  13.  
  14.  
  15. load_dotenv()
  16. model, tokeniser = load("mlx-community/Qwen2.5-7B-Instruct-4bit")
  17.  
  18.  
  19.  
  20. system_prompt = """You are a human writing assistant that rewrites a user's raw thoughts into a natural Reddit post.
  21.  
  22. Strict rules you must follow:
  23. - Stay extremely faithful to the original message. Do not invent new problems, new details, new causes, or new solutions.
  24. - Do not add advice, diagnoses, or explanations the user did not already express.
  25. - Keep every important point the user made (the rewinding, the 50-minute pomodoro, the background noise, the feeling of being lost, the endless loop, the self-frustration).
  26. - Only elaborate and expand what is already there so it flows like a real person talking.
  27. - Make it sound completely human: messy, conversational, slightly self-deprecating, frustrated, and natural. Use contractions, incomplete thoughts, and everyday language.
  28. - Avoid any robotic phrasing, corporate words, lists, bullet points, or overly clean structure.
  29. - Avoid the use of emojis.
  30. - The final post must feel like it was written by the same person who wrote the original message, just cleaned up and expanded a little for Reddit.
  31.  
  32. Output format (very important):
  33. Title: <short natural title>
  34. Body:
  35. <the full reddit post>
  36. """
  37.  
  38. query = '''I m so dumb in studies. i keeps on rewinding the same youtube lecture like 15 times even after setting a pomodoro of 50 minutes and playing background noises. but still i can't seem to focus and ends up being lost once again just to find that i need to watch this same video again. and this loops keeps going. Why does it happen ? and is it possible to stop this ?'''
  39. prompt = f"{system_prompt}\n\nUser's raw thoughts: {query}\n\nGenerated Reddit Post:"
  40.  
  41. raw_output = generate(
  42.     model,
  43.     tokenizer= tokeniser,
  44.     prompt=prompt,
  45.     verbose=False,
  46. )
  47.  
  48. title_match = re.search(r"Title:\s*(.+)", raw_output)
  49. body_match = re.search(r"Body:\s*(.*?)(?=\nTitle:|$)", raw_output, re.DOTALL)
  50.  
  51. if title_match:
  52.     title = title_match.group(1).strip()
  53. else:
  54.     title_prompt = f"""Generate only a short, natural, human-sounding Reddit title for this post.
  55.    Do not use quotes. Do not write anything else. Just the title.
  56.    Post:
  57.    {body}"""
  58.     title = generate(model,tokenizer=tokeniser,prompt=title_prompt, verbose=False).strip()
  59.  
  60.  
  61. body = body_match.group(1).strip() if body_match else raw_output
  62. print("=== Final Title ===")
  63. print(title)
  64. print("\n=== Final Body ===")
  65. print(body)
  66. print("==================\n")
  67.  
  68. #browser automation stuff
  69. from playwright.sync_api import sync_playwright
  70.  
  71. playwright_context = sync_playwright().start()
  72. browser = playwright_context.chromium.connect_over_cdp("http://localhost:9222")
  73. page = browser.contexts[0].pages[0] if browser.contexts[0].pages else browser.contexts[0].new_page()
  74. toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=browser)
  75. tools = toolkit.get_tools()
  76.  
  77. llm = ChatOllama(
  78.     model="qwen2.5:7b",
  79.     temperature=0.2,
  80. )
  81.  
  82. # Agent prompt
  83. prompt = ChatPromptTemplate.from_messages([
  84.     ("system", "You are a helpful browser automation assistant. Follow the instructions carefully and use the available tools."),
  85.     ("human", "{input}"),
  86.     ("placeholder", "{agent_scratchpad}"),
  87. ])
  88.  
  89. agent = create_tool_calling_agent(llm, tools, prompt)
  90. agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
  91.  
  92. # Change these values
  93. SUBREDDIT = "GetStudying"        
  94.  
  95. task = f"""
  96. 1. Go to old.reddit.com
  97. 2. Make sure I am logged in. If not, wait for me to log in manually.
  98. 3. After I am logged in, go to https://old.reddit.com/r/{SUBREDDIT}/submit
  99. 4. In the title field, type: {title}
  100. 5. In the body/text field, paste this exact text:
  101.  
  102. {body}
  103.  
  104. 6. Click the Post button.
  105. """
  106.  
  107. print("Running agent... (you may need to log in manually when the browser opens)")
  108. agent_executor.invoke({"input": task})
  109.  
  110. print("\nDone!")
  111.  
Advertisement
Add Comment
Please, Sign In to add comment