Spaces:
Running
Running
irina.maksimova1
commited on
Commit
·
639e75e
1
Parent(s):
acf097c
remove cookie
Browse files- app.py +73 -51
- prompts.yaml +1 -0
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,38 +1,29 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from tools.final_answer import FinalAnswerTool
|
6 |
import instaloader
|
7 |
import os
|
8 |
-
import requests
|
9 |
from PIL import Image
|
10 |
-
from io import BytesIO
|
11 |
import aiohttp
|
12 |
import asyncio
|
13 |
from aiofiles import open as aio_open
|
14 |
-
|
|
|
|
|
15 |
|
16 |
from Gradio_UI import GradioUI
|
17 |
|
18 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
19 |
-
@tool
|
20 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
21 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
22 |
-
"""A tool that does nothing yet
|
23 |
-
Args:
|
24 |
-
arg1: the first argument
|
25 |
-
arg2: the second argument
|
26 |
-
"""
|
27 |
-
return "What magic will you build ?"
|
28 |
|
29 |
@tool
|
30 |
-
|
31 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
32 |
"""A tool that analyzes your Instagram profile and describes how you appear to people seeing it for the first time.
|
33 |
Args:
|
34 |
username: The Instagram username of the person whose profile needs to be analyzed. Always strats from "@". Example: @irusvvirus.
|
35 |
"""
|
|
|
|
|
36 |
DATA_ROOT = f"data/{username}"
|
37 |
MAX_SIZE = (144, 192)
|
38 |
TOP_K_POSTS = 12
|
@@ -40,31 +31,32 @@ async def my_insta_analizer(username:str)-> str: #it's import to specify the ret
|
|
40 |
|
41 |
# Login to Insta
|
42 |
L = instaloader.Instaloader()
|
43 |
-
L.load_session("hfia2025", {
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
})
|
50 |
-
|
51 |
|
52 |
# Get target profile
|
53 |
profile = instaloader.Profile.from_username(L.context, username)
|
54 |
|
55 |
# Read general info
|
56 |
user_meta = {
|
57 |
-
"
|
58 |
-
"
|
59 |
-
"
|
60 |
-
"
|
61 |
-
"
|
62 |
-
"
|
63 |
-
"
|
64 |
}
|
65 |
if user_meta["private"]:
|
66 |
return "This profile is private. We are not allowed to access it."
|
67 |
|
|
|
|
|
68 |
# Scrape posts
|
69 |
posts = profile.get_posts()
|
70 |
|
@@ -89,26 +81,18 @@ async def my_insta_analizer(username:str)-> str: #it's import to specify the ret
|
|
89 |
}
|
90 |
return meta
|
91 |
|
92 |
-
async def process_posts(
|
93 |
tasks = []
|
94 |
async with aiohttp.ClientSession() as session:
|
95 |
-
for i, post in enumerate(
|
96 |
-
if i ==
|
97 |
break
|
98 |
print(f"Downloading post {i}...")
|
99 |
tasks.append(download_post(post, session))
|
100 |
-
result = await asyncio.gather(*tasks)
|
101 |
return result
|
102 |
-
|
103 |
-
def run_posts_scraping_async(profile):
|
104 |
-
loop = asyncio.get_event_loop()
|
105 |
-
if loop.is_running():
|
106 |
-
future = asyncio.ensure_future(process_posts(profile))
|
107 |
-
return future
|
108 |
-
else:
|
109 |
-
return asyncio.run(process_posts(profile)) # Works in standalone scripts
|
110 |
|
111 |
-
posts_meta =
|
112 |
|
113 |
def create_image_grid(images, grid_size=(3, 4)):
|
114 |
cols, rows = grid_size
|
@@ -154,10 +138,48 @@ async def my_insta_analizer(username:str)-> str: #it's import to specify the ret
|
|
154 |
img.thumbnail(MAX_SIZE)
|
155 |
posts_images.append(img)
|
156 |
posts_image = create_image_grid(posts_images)
|
157 |
-
|
158 |
user_meta["posts"] = posts_meta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
-
return f"I recieved posts from {username} N_posts={len(posts_images)} Shape={posts_image.shape} meta={user_meta}"
|
161 |
|
162 |
@tool
|
163 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -181,10 +203,10 @@ final_answer = FinalAnswerTool()
|
|
181 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
182 |
|
183 |
model = HfApiModel(
|
184 |
-
max_tokens=2096,
|
185 |
-
temperature=0.5,
|
186 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
187 |
-
custom_role_conversions=None,
|
188 |
)
|
189 |
|
190 |
|
@@ -196,7 +218,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
196 |
|
197 |
agent = CodeAgent(
|
198 |
model=model,
|
199 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
200 |
max_steps=6,
|
201 |
verbosity_level=1,
|
202 |
grammar=None,
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from tools.final_answer import FinalAnswerTool
|
6 |
import instaloader
|
7 |
import os
|
|
|
8 |
from PIL import Image
|
|
|
9 |
import aiohttp
|
10 |
import asyncio
|
11 |
from aiofiles import open as aio_open
|
12 |
+
from qwen_vl_utils import process_vision_info
|
13 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
14 |
+
import torch
|
15 |
|
16 |
from Gradio_UI import GradioUI
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
@tool
|
20 |
+
def my_insta_analizer(username: str) -> str: #it's import to specify the return type
|
|
|
21 |
"""A tool that analyzes your Instagram profile and describes how you appear to people seeing it for the first time.
|
22 |
Args:
|
23 |
username: The Instagram username of the person whose profile needs to be analyzed. Always strats from "@". Example: @irusvvirus.
|
24 |
"""
|
25 |
+
# return "I have analized your priffile - you are the best"
|
26 |
+
username = username.replace("@", "")
|
27 |
DATA_ROOT = f"data/{username}"
|
28 |
MAX_SIZE = (144, 192)
|
29 |
TOP_K_POSTS = 12
|
|
|
31 |
|
32 |
# Login to Insta
|
33 |
L = instaloader.Instaloader()
|
34 |
+
# L.load_session("hfia2025", {
|
35 |
+
# "csrftoken": "EvyQWJTLWfbLy00C1h2hJoMmW3V002ik",
|
36 |
+
# "sessionid": "72565382956%3AbXg6LSEaqDXogy%3A21%3AAYc8jNkM18-P4t4l7dokdj3NK5odeGP6xfCwMiL0NA",
|
37 |
+
# "ds_user_id": "72565382956",
|
38 |
+
# "mid": "Zz7yUQAEAAHHANNrTkDrJG-KA05E",
|
39 |
+
# "ig_did": "44A88963-6613-41AA-93F0-418DF87BDA72"
|
40 |
+
# })
|
|
|
41 |
|
42 |
# Get target profile
|
43 |
profile = instaloader.Profile.from_username(L.context, username)
|
44 |
|
45 |
# Read general info
|
46 |
user_meta = {
|
47 |
+
"username": profile.username,
|
48 |
+
"full Name": profile.full_name,
|
49 |
+
"bio": profile.biography,
|
50 |
+
"followers": profile.followers,
|
51 |
+
"followees": profile.followees,
|
52 |
+
"private": profile.is_private,
|
53 |
+
"verified": profile.is_verified,
|
54 |
}
|
55 |
if user_meta["private"]:
|
56 |
return "This profile is private. We are not allowed to access it."
|
57 |
|
58 |
+
# return f"I loged to inst && Profile {username} looks great && I recieved next user meta={user_meta}!"
|
59 |
+
|
60 |
# Scrape posts
|
61 |
posts = profile.get_posts()
|
62 |
|
|
|
81 |
}
|
82 |
return meta
|
83 |
|
84 |
+
async def process_posts(posts):
|
85 |
tasks = []
|
86 |
async with aiohttp.ClientSession() as session:
|
87 |
+
for i, post in enumerate(posts):
|
88 |
+
if i == TOP_K_POSTS:
|
89 |
break
|
90 |
print(f"Downloading post {i}...")
|
91 |
tasks.append(download_post(post, session))
|
92 |
+
result = await asyncio.gather(*tasks)
|
93 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
posts_meta = asyncio.run(process_posts(posts))
|
96 |
|
97 |
def create_image_grid(images, grid_size=(3, 4)):
|
98 |
cols, rows = grid_size
|
|
|
138 |
img.thumbnail(MAX_SIZE)
|
139 |
posts_images.append(img)
|
140 |
posts_image = create_image_grid(posts_images)
|
|
|
141 |
user_meta["posts"] = posts_meta
|
142 |
+
|
143 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
144 |
+
"Qwen/Qwen2.5-VL-7B-Instruct",
|
145 |
+
torch_dtype=torch.bfloat16,
|
146 |
+
attn_implementation="flash_attention_2",
|
147 |
+
device_map="auto",
|
148 |
+
)
|
149 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
|
150 |
+
messages = [
|
151 |
+
{
|
152 |
+
"role": "user",
|
153 |
+
"content": [
|
154 |
+
{
|
155 |
+
"type": "image",
|
156 |
+
"image": posts_image,
|
157 |
+
},
|
158 |
+
{"type": "text", "text": "Describe this image."},
|
159 |
+
],
|
160 |
+
}
|
161 |
+
]
|
162 |
+
text = processor.apply_chat_template(
|
163 |
+
messages, tokenize=False, add_generation_prompt=True
|
164 |
+
)
|
165 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
166 |
+
inputs = processor(
|
167 |
+
text=[text],
|
168 |
+
images=image_inputs,
|
169 |
+
videos=video_inputs,
|
170 |
+
padding=True,
|
171 |
+
return_tensors="pt",
|
172 |
+
)
|
173 |
+
inputs = inputs.to(model.device)
|
174 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
175 |
+
generated_ids_trimmed = [
|
176 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
177 |
+
]
|
178 |
+
output_text = processor.batch_decode(
|
179 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
180 |
+
)
|
181 |
+
return output_text
|
182 |
|
|
|
183 |
|
184 |
@tool
|
185 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
203 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
204 |
|
205 |
model = HfApiModel(
|
206 |
+
max_tokens=2096,
|
207 |
+
temperature=0.5,
|
208 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
209 |
+
custom_role_conversions=None,
|
210 |
)
|
211 |
|
212 |
|
|
|
218 |
|
219 |
agent = CodeAgent(
|
220 |
model=model,
|
221 |
+
tools=[my_insta_analizer, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
|
222 |
max_steps=6,
|
223 |
verbosity_level=1,
|
224 |
grammar=None,
|
prompts.yaml
CHANGED
@@ -11,6 +11,7 @@
|
|
11 |
|
12 |
You have access to the following tools:
|
13 |
Tool Name: my_insta_analizer, Description: A tool that analyzes your Instagram profile and describes how you appear to people seeing it for the first time, Arguments: username: str, Outputs: str
|
|
|
14 |
|
15 |
|
16 |
Here are a few examples using notional tools:
|
|
|
11 |
|
12 |
You have access to the following tools:
|
13 |
Tool Name: my_insta_analizer, Description: A tool that analyzes your Instagram profile and describes how you appear to people seeing it for the first time, Arguments: username: str, Outputs: str
|
14 |
+
Tool Name: get_current_time_in_timezone, Description: A tool that fetches the current local time in a specified timezone, Arguments: timezone: str, Outputs: str
|
15 |
|
16 |
|
17 |
Here are a few examples using notional tools:
|
requirements.txt
CHANGED
@@ -7,4 +7,8 @@ instaloader
|
|
7 |
pillow
|
8 |
aiohttp
|
9 |
aiofiles
|
10 |
-
matplotlib
|
|
|
|
|
|
|
|
|
|
7 |
pillow
|
8 |
aiohttp
|
9 |
aiofiles
|
10 |
+
matplotlib
|
11 |
+
transformers @ git+https://github.com/huggingface/transformers.git
|
12 |
+
torch
|
13 |
+
qwen-vl-utils
|
14 |
+
torchvision
|