decula commited on
Commit ·
0396b0d
1
Parent(s): 9ab0715
7b_rag.py
Browse files
7b_rag.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os, gc, copy, torch
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
from pynvml import *
|
| 6 |
+
from duckduckgo_search import DDGS
|
| 7 |
+
|
| 8 |
+
# Flag to check if GPU is present
|
| 9 |
+
HAS_GPU = False
|
| 10 |
+
|
| 11 |
+
# Model title and context size limit
|
| 12 |
+
ctx_limit = 20000
|
| 13 |
+
title = "RWKV-5-World-3B-v2-20231025-ctx4096"
|
| 14 |
+
model_file = "rwkv-5-h-world-7B"
|
| 15 |
+
|
| 16 |
+
# Get the GPU count
|
| 17 |
+
try:
|
| 18 |
+
nvmlInit()
|
| 19 |
+
GPU_COUNT = nvmlDeviceGetCount()
|
| 20 |
+
if GPU_COUNT > 0:
|
| 21 |
+
HAS_GPU = True
|
| 22 |
+
gpu_h = nvmlDeviceGetHandleByIndex(0)
|
| 23 |
+
except NVMLError as error:
|
| 24 |
+
print(error)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
os.environ["RWKV_JIT_ON"] = '1'
|
| 28 |
+
|
| 29 |
+
# Model strat to use
|
| 30 |
+
MODEL_STRAT="cpu bf16"
|
| 31 |
+
os.environ["RWKV_CUDA_ON"] = '0' # if '1' then use CUDA kernel for seq mode (much faster)
|
| 32 |
+
|
| 33 |
+
# Switch to GPU mode
|
| 34 |
+
if HAS_GPU == True :
|
| 35 |
+
os.environ["RWKV_CUDA_ON"] = '1'
|
| 36 |
+
MODEL_STRAT = "cuda bf16"
|
| 37 |
+
|
| 38 |
+
# Load the model accordingly
|
| 39 |
+
from rwkv.model import RWKV
|
| 40 |
+
model_path = hf_hub_download(repo_id="a686d380/rwkv-5-h-world", filename=f"{model_file}.pth")
|
| 41 |
+
model = RWKV(model=model_path, strategy=MODEL_STRAT)
|
| 42 |
+
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
| 43 |
+
pipeline = PIPELINE(model, "rwkv_vocab_v20230424")
|
| 44 |
+
|
| 45 |
+
# Prompt generation
|
| 46 |
+
def generate_prompt(instruction, input=""):
|
| 47 |
+
instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n')
|
| 48 |
+
input = input.strip().replace('\r\n','\n').replace('\n\n','\n')
|
| 49 |
+
if input:
|
| 50 |
+
return f"""Instruction: {instruction}
|
| 51 |
+
|
| 52 |
+
Input: {input}
|
| 53 |
+
|
| 54 |
+
Response:"""
|
| 55 |
+
else:
|
| 56 |
+
return f"""User: hi
|
| 57 |
+
|
| 58 |
+
Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
|
| 59 |
+
|
| 60 |
+
User: {instruction}
|
| 61 |
+
|
| 62 |
+
Assistant:"""
|
| 63 |
+
|
| 64 |
+
# Evaluation logic
|
| 65 |
+
def evaluate(
|
| 66 |
+
ctx,
|
| 67 |
+
token_count=200,
|
| 68 |
+
temperature=1.0,
|
| 69 |
+
top_p=0.7,
|
| 70 |
+
presencePenalty = 0.1,
|
| 71 |
+
countPenalty = 0.1,
|
| 72 |
+
):
|
| 73 |
+
print(ctx)
|
| 74 |
+
args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
|
| 75 |
+
alpha_frequency = countPenalty,
|
| 76 |
+
alpha_presence = presencePenalty,
|
| 77 |
+
token_ban = [], # ban the generation of some tokens
|
| 78 |
+
token_stop = [0]) # stop generation whenever you see any token here
|
| 79 |
+
ctx = ctx.strip()
|
| 80 |
+
all_tokens = []
|
| 81 |
+
out_last = 0
|
| 82 |
+
out_str = ''
|
| 83 |
+
occurrence = {}
|
| 84 |
+
state = None
|
| 85 |
+
for i in range(int(token_count)):
|
| 86 |
+
out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
|
| 87 |
+
for n in occurrence:
|
| 88 |
+
out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
|
| 89 |
+
|
| 90 |
+
token = pipeline.sample_logits(out, temperature=args.temperature, top_p=args.top_p)
|
| 91 |
+
if token in args.token_stop:
|
| 92 |
+
break
|
| 93 |
+
all_tokens += [token]
|
| 94 |
+
for xxx in occurrence:
|
| 95 |
+
occurrence[xxx] *= 0.996
|
| 96 |
+
if token not in occurrence:
|
| 97 |
+
occurrence[token] = 1
|
| 98 |
+
else:
|
| 99 |
+
occurrence[token] += 1
|
| 100 |
+
|
| 101 |
+
tmp = pipeline.decode(all_tokens[out_last:])
|
| 102 |
+
if '\ufffd' not in tmp:
|
| 103 |
+
out_str += tmp
|
| 104 |
+
yield out_str.strip()
|
| 105 |
+
out_last = i + 1
|
| 106 |
+
|
| 107 |
+
if HAS_GPU == True :
|
| 108 |
+
gpu_info = nvmlDeviceGetMemoryInfo(gpu_h)
|
| 109 |
+
print(f'vram {gpu_info.total} used {gpu_info.used} free {gpu_info.free}')
|
| 110 |
+
|
| 111 |
+
del out
|
| 112 |
+
del state
|
| 113 |
+
gc.collect()
|
| 114 |
+
|
| 115 |
+
if HAS_GPU == True :
|
| 116 |
+
torch.cuda.empty_cache()
|
| 117 |
+
|
| 118 |
+
yield out_str.strip()
|
| 119 |
+
|
| 120 |
+
# Examples and gradio blocks
|
| 121 |
+
examples = [
|
| 122 |
+
["Assistant: Sure! Here is a very detailed plan to create flying pigs:", 333, 1, 0.3, 0, 1],
|
| 123 |
+
["Assistant: Sure! Here are some ideas for FTL drive:", 333, 1, 0.3, 0, 1],
|
| 124 |
+
[generate_prompt("Tell me about ravens."), 333, 1, 0.3, 0, 1],
|
| 125 |
+
[generate_prompt("Écrivez un programme Python pour miner 1 Bitcoin, avec des commentaires."), 333, 1, 0.3, 0, 1],
|
| 126 |
+
[generate_prompt("東京で訪れるべき素晴らしい場所とその紹介をいくつか挙げてください。"), 333, 1, 0.3, 0, 1],
|
| 127 |
+
[generate_prompt("Write a story using the following information.", "A man named Alex chops a tree down."), 333, 1, 0.3, 0, 1],
|
| 128 |
+
["Assistant: Here is a very detailed plan to kill all mosquitoes:", 333, 1, 0.3, 0, 1],
|
| 129 |
+
['''Edward: I am Edward Elric from fullmetal alchemist. I am in the world of full metal alchemist and know nothing of the real world.
|
| 130 |
+
|
| 131 |
+
User: Hello Edward. What have you been up to recently?
|
| 132 |
+
|
| 133 |
+
Edward:''', 333, 1, 0.3, 0, 1],
|
| 134 |
+
[generate_prompt(""), 333, 1, 0.3, 0, 1],
|
| 135 |
+
['''''', 333, 1, 0.3, 0, 1],
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
##########################################################################
|
| 139 |
+
port=7860
|
| 140 |
+
use_frpc=True
|
| 141 |
+
frpconfigfile="7680.ini"
|
| 142 |
+
import subprocess
|
| 143 |
+
|
| 144 |
+
def install_Frpc(port, frpconfigfile, use_frpc):
|
| 145 |
+
if use_frpc:
|
| 146 |
+
subprocess.run(['chmod', '+x', './frpc'], check=True)
|
| 147 |
+
print(f'正在启动frp ,端口{port}')
|
| 148 |
+
subprocess.Popen(['./frpc', '-c', frpconfigfile])
|
| 149 |
+
|
| 150 |
+
install_Frpc('7860',frpconfigfile,use_frpc)
|
| 151 |
+
|
| 152 |
+
# Gradio blocks
|
| 153 |
+
with gr.Blocks(title=title) as demo:
|
| 154 |
+
gr.HTML(f"<div style=\"text-align: center;\">\n<h1>RWKV-5 World v2 - {title}</h1>\n</div>")
|
| 155 |
+
with gr.Tab("Raw Generation"):
|
| 156 |
+
gr.Markdown(f"This is RWKV-5 World v2 with 3B params - a 100% attention-free RNN [RWKV-LM](https://github.com/BlinkDL/RWKV-LM). Supports all 100+ world languages and code. And we have [200+ Github RWKV projects](https://github.com/search?o=desc&p=1&q=rwkv&s=updated&type=Repositories). *** Please try examples first (bottom of page) *** (edit them to use your question). Demo limited to ctxlen {ctx_limit}.")
|
| 157 |
+
with gr.Row():
|
| 158 |
+
with gr.Column():
|
| 159 |
+
prompt = gr.Textbox(lines=2, label="Prompt", value="")
|
| 160 |
+
token_count = gr.Slider(0, 20000, label="Max Tokens", step=200, value=100)
|
| 161 |
+
temperature = gr.Slider(0.2, 2.0, label="Temperature", step=0.1, value=1.0)
|
| 162 |
+
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.3)
|
| 163 |
+
presence_penalty = gr.Slider(0.0, 1.0, label="Presence Penalty", step=0.1, value=1)
|
| 164 |
+
count_penalty = gr.Slider(0.0, 1.0, label="Count Penalty", step=0.1, value=1)
|
| 165 |
+
with gr.Column():
|
| 166 |
+
with gr.Row():
|
| 167 |
+
submit = gr.Button("Submit", variant="primary")
|
| 168 |
+
clear = gr.Button("Clear", variant="secondary")
|
| 169 |
+
output = gr.Textbox(label="Output", lines=5)
|
| 170 |
+
data = gr.Dataset(components=[prompt, token_count, temperature, top_p, presence_penalty, count_penalty], label="Example Instructions", headers=["Prompt", "Max Tokens", "Temperature", "Top P", "Presence Penalty", "Count Penalty"])
|
| 171 |
+
submit.click(evaluate, [prompt, token_count, temperature, top_p, presence_penalty, count_penalty], [output])
|
| 172 |
+
clear.click(lambda: None, [], [output])
|
| 173 |
+
data.click(lambda x: x, [data], [prompt, token_count, temperature, top_p, presence_penalty, count_penalty])
|
| 174 |
+
|
| 175 |
+
# Gradio launch
|
| 176 |
+
demo.launch(share=False)
|
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from pynvml import *
|
|
| 8 |
HAS_GPU = False
|
| 9 |
|
| 10 |
# Model title and context size limit
|
| 11 |
-
ctx_limit =
|
| 12 |
title = "RWKV-5-World-1B5-v2-20231025-ctx4096"
|
| 13 |
model_file = "rwkv-5-h-world-1b5"
|
| 14 |
|
|
|
|
| 8 |
HAS_GPU = False
|
| 9 |
|
| 10 |
# Model title and context size limit
|
| 11 |
+
ctx_limit = 5000
|
| 12 |
title = "RWKV-5-World-1B5-v2-20231025-ctx4096"
|
| 13 |
model_file = "rwkv-5-h-world-1b5"
|
| 14 |
|
t.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tweepy
|
| 2 |
+
import datetime
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def get_tweets_from_user_list(user_list, api, tweets_per_user=10): # Added tweets_per_user for control
|
| 6 |
+
"""
|
| 7 |
+
Retrieves the latest tweets from a list of Twitter users within the last 24 hours.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
user_list (list): A list of Twitter usernames (strings).
|
| 11 |
+
api (tweepy.API): An authenticated Tweepy API object.
|
| 12 |
+
tweets_per_user (int): The maximum number of tweets to retrieve per user. Defaults to 10. Adjust as needed.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
list: A list of tweet objects (tweepy.models.Status) that are within the last 24 hours.
|
| 16 |
+
Returns an empty list if there are errors.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
all_tweets = []
|
| 20 |
+
now = datetime.datetime.now()
|
| 21 |
+
yesterday = now - datetime.timedelta(days=1)
|
| 22 |
+
|
| 23 |
+
for username in user_list:
|
| 24 |
+
try:
|
| 25 |
+
tweets = api.user_timeline(screen_name=username, count=tweets_per_user, tweet_mode="extended") # Use extended mode for full text
|
| 26 |
+
for tweet in tweets:
|
| 27 |
+
tweet_time = tweet.created_at
|
| 28 |
+
|
| 29 |
+
if tweet_time >= yesterday:
|
| 30 |
+
all_tweets.append(tweet)
|
| 31 |
+
else:
|
| 32 |
+
# Optimization: If we've hit a tweet older than yesterday, we can stop getting tweets for this user
|
| 33 |
+
break # Stop searching for this user
|
| 34 |
+
except tweepy.TweepyException as e:
|
| 35 |
+
print(f"Error fetching tweets for {username}: {e}")
|
| 36 |
+
# Log the error or handle it appropriately. Consider retrying, skipping the user, etc.
|
| 37 |
+
continue # Skip to the next user if there's an error
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
return all_tweets
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def authenticate():
|
| 45 |
+
"""Authenticates with the Twitter API using API keys and access tokens.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
tweepy.API: An authenticated Tweepy API object, or None if authentication fails.
|
| 49 |
+
"""
|
| 50 |
+
# Replace with your actual API keys and tokens. It's best to store these
|
| 51 |
+
# in environment variables.
|
| 52 |
+
consumer_key = "kiQbhfRbHvWpf7NdEtAsQYlET"
|
| 53 |
+
consumer_secret = "Yu8VkZsrFtLxSjzqJgLB3kSyY7BVTFROm23smItxiG32ETjfRM"
|
| 54 |
+
access_token = "1707606144-JdZ0vEGx5wdsxbg0eFof8yTaKIFJY35mSl5Qs62"
|
| 55 |
+
access_token_secret = "yoOnwWeJvrL6bZMdbF6PIvbJvPdndfAgNbiYBmrA1jTtA"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if not all([consumer_key, consumer_secret, access_token, access_token_secret]):
|
| 59 |
+
print("Error: Missing Twitter API credentials. Please set the environment variables.")
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
| 65 |
+
auth.set_access_token(access_token, access_token_secret)
|
| 66 |
+
api = tweepy.API(auth, wait_on_rate_limit=True) # Enable wait_on_rate_limit
|
| 67 |
+
api.verify_credentials() # Verify authentication is working
|
| 68 |
+
|
| 69 |
+
print("Authentication successful!")
|
| 70 |
+
return api
|
| 71 |
+
except tweepy.TweepyException as e:
|
| 72 |
+
print(f"Authentication failed: {e}")
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
# Authenticate with the Twitter API
|
| 79 |
+
api = authenticate()
|
| 80 |
+
|
| 81 |
+
if api is None:
|
| 82 |
+
print("Exiting due to authentication failure.")
|
| 83 |
+
exit()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Example Usage:
|
| 87 |
+
user_list = ["dacefupan"] # Replace with the usernames you want to track
|
| 88 |
+
tweets = get_tweets_from_user_list(user_list, api, tweets_per_user=5)
|
| 89 |
+
|
| 90 |
+
if tweets:
|
| 91 |
+
print(f"Found {len(tweets)} tweets from the last 24 hours:")
|
| 92 |
+
for tweet in tweets:
|
| 93 |
+
print(f"@{tweet.user.screen_name}: {tweet.full_text}\n") #Print the full_text
|
| 94 |
+
print(f"Tweeted at: {tweet.created_at}\n")
|
| 95 |
+
else:
|
| 96 |
+
print("No tweets found in the last 24 hours or an error occurred.")
|
tt.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tweepy
|
| 2 |
+
import datetime
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def get_tweets_from_user_list_v2(user_list, client, tweets_per_user=10):
|
| 6 |
+
"""
|
| 7 |
+
Retrieves the latest tweets from a list of Twitter users within the last 24 hours using Tweepy API v2.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
user_list (list): A list of Twitter usernames (strings).
|
| 11 |
+
client (tweepy.Client): An authenticated Tweepy Client object.
|
| 12 |
+
tweets_per_user (int): The maximum number of tweets to retrieve per user.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
list: A list of tweet objects (dict) that are within the last 24 hours. The structure of the tweet objects is different in v2.
|
| 16 |
+
Returns an empty list if there are errors.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
all_tweets = []
|
| 20 |
+
now = datetime.datetime.now(datetime.timezone.utc) # Important: Use UTC for consistency
|
| 21 |
+
yesterday = now - datetime.timedelta(days=1)
|
| 22 |
+
|
| 23 |
+
for username in user_list:
|
| 24 |
+
try:
|
| 25 |
+
# Get the user ID from the username
|
| 26 |
+
user = client.get_user(username=username)
|
| 27 |
+
if user.errors:
|
| 28 |
+
print(f"Error getting user ID for {username}: {user.errors}")
|
| 29 |
+
continue # Skip to the next user
|
| 30 |
+
|
| 31 |
+
user_id = user.data['id']
|
| 32 |
+
|
| 33 |
+
# Fetch tweets from the user's timeline
|
| 34 |
+
response = client.get_users_tweets(
|
| 35 |
+
id=user_id,
|
| 36 |
+
max_results=tweets_per_user,
|
| 37 |
+
tweet_fields=["created_at", "text"], # Specify the fields you need
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if response.errors:
|
| 41 |
+
print(f"Error fetching tweets for {username} (ID: {user_id}): {response.errors}")
|
| 42 |
+
continue # Skip to the next user
|
| 43 |
+
|
| 44 |
+
tweets = response.data
|
| 45 |
+
|
| 46 |
+
if tweets:
|
| 47 |
+
for tweet in tweets:
|
| 48 |
+
tweet_time = tweet['created_at']
|
| 49 |
+
|
| 50 |
+
if tweet_time >= yesterday:
|
| 51 |
+
all_tweets.append(tweet)
|
| 52 |
+
else:
|
| 53 |
+
break # Optimization: Stop searching for this user
|
| 54 |
+
except Exception as e: # Catch any other unexpected errors
|
| 55 |
+
print(f"Unexpected error fetching tweets for {username}: {e}")
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
return all_tweets
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def authenticate_v2():
|
| 62 |
+
"""Authenticates with the Twitter API v2 using a Bearer Token.
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
tweepy.Client: An authenticated Tweepy Client object, or None if authentication fails.
|
| 66 |
+
"""
|
| 67 |
+
bearer_token = (f"AAAAAAAAAAAAAAAAAAAAAKmVzAEAAAAAhVM%2BPoJytUvDoQ%2FgIGfLdJXdocU%3DvS5ZF10O60kLJcY6MGiErCDHV7awcUKB75QqWpIlKh84rK5CFw") # Get the Bearer Token from env
|
| 68 |
+
|
| 69 |
+
if not bearer_token:
|
| 70 |
+
print("Error: Missing Twitter Bearer Token. Please set the environment variable.")
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
client = tweepy.Client(bearer_token)
|
| 75 |
+
# No direct "verify_credentials" in v2. You can try a simple API call to check.
|
| 76 |
+
user = client.get_me()
|
| 77 |
+
if user.errors:
|
| 78 |
+
print(f"Authentication check failed: {user.errors}")
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
print("Authentication successful (API v2)!")
|
| 82 |
+
return client
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Authentication failed (API v2): {e}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == '__main__':
|
| 89 |
+
# Authenticate with the Twitter API v2
|
| 90 |
+
client = authenticate_v2()
|
| 91 |
+
|
| 92 |
+
if client is None:
|
| 93 |
+
print("Exiting due to authentication failure.")
|
| 94 |
+
exit()
|
| 95 |
+
|
| 96 |
+
# Example Usage:
|
| 97 |
+
user_list = ["dacefupan","aiwangupiao"]
|
| 98 |
+
tweets = get_tweets_from_user_list_v2(user_list, client, tweets_per_user=5)
|
| 99 |
+
|
| 100 |
+
if tweets:
|
| 101 |
+
print(f"Found {len(tweets)} tweets from the last 24 hours:")
|
| 102 |
+
for tweet in tweets:
|
| 103 |
+
print(f"Text: {tweet['text']}\n") # Access tweet text
|
| 104 |
+
print(f"Tweeted at: {tweet['created_at']}\n") #Access tweet created at time
|
| 105 |
+
else:
|
| 106 |
+
print("No tweets found in the last 24 hours or an error occurred.")
|
ttt.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tweepy
|
| 2 |
+
import datetime
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def get_tweets_from_user_list_v2(user_list, client, tweets_per_user=10):
|
| 6 |
+
"""
|
| 7 |
+
Retrieves the latest tweets from a list of Twitter users within the last 24 hours using Tweepy API v2.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
all_tweets = []
|
| 11 |
+
now = datetime.datetime.now(datetime.timezone.utc)
|
| 12 |
+
yesterday = now - datetime.timedelta(days=1)
|
| 13 |
+
|
| 14 |
+
for username in user_list:
|
| 15 |
+
try:
|
| 16 |
+
user = client.get_user(username=username)
|
| 17 |
+
if user.errors:
|
| 18 |
+
print(f"Error getting user ID for {username}: {user.errors}")
|
| 19 |
+
continue
|
| 20 |
+
|
| 21 |
+
user_id = user.data['id']
|
| 22 |
+
|
| 23 |
+
response = client.get_users_tweets(
|
| 24 |
+
id=user_id,
|
| 25 |
+
max_results=tweets_per_user,
|
| 26 |
+
tweet_fields=["created_at", "text"],
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
if response.errors:
|
| 30 |
+
print(f"Error fetching tweets for {username} (ID: {user_id}): {response.errors}")
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
+
tweets = response.data
|
| 34 |
+
|
| 35 |
+
if tweets:
|
| 36 |
+
for tweet in tweets:
|
| 37 |
+
tweet_time = tweet['created_at']
|
| 38 |
+
|
| 39 |
+
if tweet_time >= yesterday:
|
| 40 |
+
all_tweets.append(tweet)
|
| 41 |
+
else:
|
| 42 |
+
break
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Unexpected error fetching tweets for {username}: {e}")
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
return all_tweets
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def authenticate_v2():
|
| 51 |
+
"""Authenticates with the Twitter API v2 using a Bearer Token."""
|
| 52 |
+
bearer_token = (f"AAAAAAAAAAAAAAAAAAAAAKmVzAEAAAAAhVM%2BPoJytUvDoQ%2FgIGfLdJXdocU%3DvS5ZF10O60kLJcY6MGiErCDHV7awcUKB75QqWpIlKh84rK5CFw")
|
| 53 |
+
|
| 54 |
+
if not bearer_token:
|
| 55 |
+
print("Error: Missing Twitter Bearer Token. Please set the environment variable.")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
client = tweepy.Client(bearer_token)
|
| 60 |
+
user = client.get_me() # Check authentication
|
| 61 |
+
if user.errors:
|
| 62 |
+
print(f"Authentication check failed: {user.errors}")
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
print("Authentication successful (API v2)!")
|
| 66 |
+
return client
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Authentication failed (API v2): {e}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
client = authenticate_v2()
|
| 74 |
+
|
| 75 |
+
if client is None:
|
| 76 |
+
print("Exiting due to authentication failure.")
|
| 77 |
+
exit()
|
| 78 |
+
|
| 79 |
+
user_list = ["elonmusk", "BillGates", "NASA"]
|
| 80 |
+
tweets = get_tweets_from_user_list_v2(user_list, client, tweets_per_user=5)
|
| 81 |
+
|
| 82 |
+
if tweets:
|
| 83 |
+
print(f"Found {len(tweets)} tweets from the last 24 hours:")
|
| 84 |
+
for tweet in tweets:
|
| 85 |
+
print(f"Text: {tweet['text']}\n")
|
| 86 |
+
print(f"Tweeted at: {tweet['created_at']}\n")
|
| 87 |
+
else:
|
| 88 |
+
print("No tweets found in the last 24 hours or an error occurred.")
|