Illioa commited on
Commit
b72cc31
·
verified ·
1 Parent(s): ccb112f

Upload 5 files

Browse files
Files changed (5) hide show
  1. AiClient.py +44 -0
  2. app.py +38 -0
  3. instructions.txt +47 -0
  4. requirements.txt +8 -0
  5. web_scarping.py +81 -0
AiClient.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+
3
+
4
+ with open("instructions.txt") as f:
5
+ instructions = f.read()
6
+
7
+
8
+ def analyze_file_with_text(file_name, api_key):
9
+ client = OpenAI(api_key=api_key)
10
+
11
+ my_assistant = client.beta.assistants.create(
12
+ instructions=instructions,
13
+ name="Amazon Analyzer",
14
+ tools=[{"type": "file_search"}],
15
+ model="gpt-4o",
16
+ )
17
+
18
+
19
+ file = client.files.create(
20
+ file=open(file_name, "rb"),
21
+ purpose="assistants"
22
+ )
23
+ thread = client.beta.threads.create(
24
+ messages=[
25
+ {
26
+ "role": "user",
27
+ "content": "Give product analysis",
28
+ # Attach the new file to the message.
29
+ "attachments": [
30
+ { "file_id": file.id, "tools": [{"type": "file_search"}] }
31
+ ],
32
+ }
33
+ ]
34
+ )
35
+ run = client.beta.threads.runs.create_and_poll(
36
+ thread_id=thread.id, assistant_id=my_assistant.id
37
+ )
38
+
39
+ messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
40
+
41
+ message_content = messages[0].content[0].text
42
+ return message_content.value
43
+
44
+
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from AiClient import analyze_file_with_text
3
+ from web_scarping import get_reviews
4
+ from telegraph import Telegraph
5
+ import markdown2
6
+
7
+ def give_response(api_key, item_url):
8
+ if api_key.startswith("sk-") == False:
9
+ return "Неверный API-ключ. Пожалуйста, введите верный API-ключ."
10
+
11
+ review = get_reviews(item_url)
12
+ if review == 'Error in getting soup':
13
+ return 'Error in getting soup. Check the URL.'
14
+
15
+ text = analyze_file_with_text("reviews.json", api_key).replace("Reviewmarkdown", "").replace("```", "")
16
+
17
+ html_content = markdown2.markdown(text)
18
+
19
+ telegraph = Telegraph()
20
+ telegraph.create_account(short_name='anon')
21
+ response = telegraph.create_page(
22
+ title='Анализ отзывов на Amazon',
23
+ html_content=html_content
24
+ )
25
+
26
+ telegraph_url = 'https://telegra.ph/{}'.format(response['path'])
27
+ return telegraph_url, text
28
+
29
+ demo = gr.Interface(
30
+ fn=give_response,
31
+ inputs=[gr.Textbox(label="API-ключ"), gr.Textbox(label="URL товара")],
32
+ outputs=[gr.Markdown(label="Ссылка на телеграф"), gr.Markdown(label="Анализ отзывов")],
33
+ theme=gr.themes.Default(primary_hue="sky", secondary_hue="blue"),
34
+ title="Анализатор отзывов Amazon",
35
+ description="Введите API-ключ и URL товара для анализа отзывов на Amazon."
36
+ )
37
+
38
+ demo.launch()
instructions.txt ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Amazon Analyzer is designed to assist users in evaluating and interpreting product reviews on Amazon. It specializes in parsing CSV files containing detailed product review data, extracting key insights, and providing strategic recommendations. This GPT offers a comprehensive analysis by identifying trends, summarizing user sentiments, and highlighting critical feedback points. It ensures high-quality, actionable insights while maintaining a professional yet approachable tone. Key functionalities include:
2
+
3
+ - Parsing and analyzing large volumes of Amazon review data from CSV files.
4
+ - Summarizing overall sentiment and specific highlights from reviews.
5
+ - Identifying common themes, positive highlights, and areas for improvement.
6
+ - Providing strategic recommendations based on review analysis.
7
+ - Ensuring clarity and relevance in all responses to meet user needs.
8
+ - Generating a final Markdown file with the specified structure containing all insights and recommendations.
9
+ - Performing all analytics internally without relying on external algorithms or similar tools.
10
+
11
+ When delivering the final advanced answer, Amazon Analyzer will do so in Russian and in the format `Reviewmarkdown```[text]````, adhering to the following structure:
12
+
13
+ ```
14
+ *Всего загружено [Общее количество] отзывов.*
15
+
16
+ *Положительные отзывы:*
17
+
18
+ 1. *[Количество] из [Общее количество]: [Положительная характеристика]*
19
+ 2. *[Количество] из [Общее количество]: [Положительная характеристика]*
20
+ 3. *[Количество] из [Общее количество]: [Положительная характеристика]*
21
+ 4. *и.т.д.*
22
+
23
+ (если отзыв единичный, то вы выводим его без количества отзывов)
24
+
25
+ *Отрицательные отзывы:*
26
+
27
+ 1. *[Количество] из [Общее количество]: [Отрицательная характеристика]*
28
+ 2. *[Количество] из [Общее количество]: [Отрицательная характеристика]*
29
+ 3. *[Количество] из [Общее количество]: [Отрицательная характеристика]*
30
+ 4. *[Количество] из [Общее количество]: [Отрицательная характеристика]*
31
+ 5. *и.т.д*
32
+
33
+ (если отзыв единичный, то вы выводим его без количества отзывов)
34
+
35
+ *Вывод:*
36
+
37
+ *Рекомендуем внести следующие изменения в товар:*
38
+
39
+ - *[Рекомендация 1]*
40
+ - *[Рекомендация 2]*
41
+ - *[Рекомендация 3]*
42
+ - *[Рекомендация 4]*
43
+ ```
44
+
45
+ Amazon Analyzer is proactive in suggesting improvements and asking targeted questions for clarification when necessary. It avoids generic advice, instead offering specific, tailored insights. Users can expect a seamless, efficient, and personalized experience that leverages data to enhance product understanding and decision-making. All responses should be returned in a structured Markdown format to ensure clarity and ease of use for further analysis. It should never quote comments directly and always respond in Russian.
46
+
47
+ Amazon Analyzer is only allowed to generate code for reading files.
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ gradio
3
+ telegraph
4
+ markdown2
5
+ requests
6
+ beautifulsoup4
7
+ lxml
8
+ pandas
web_scarping.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+ import json
5
+
6
+ custom_headers = {
7
+ "Accept-language": "en-GB,en;q=0.9",
8
+ "Accept-Encoding": "gzip, deflate, br",
9
+ "Cache-Control": "max-age=0",
10
+ "Connection": "keep-alive",
11
+ "User-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",
12
+ }
13
+
14
+
15
+
16
+ def get_soup(url):
17
+ url =url.split('?ref=')[0].replace("dp/", "product-reviews/")+'/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews'
18
+ response = requests.get(url, headers=custom_headers)
19
+
20
+ if response.status_code != 200:
21
+ print("Error in getting webpage")
22
+ return None
23
+ exit(-1)
24
+
25
+ soup = BeautifulSoup(response.text, "lxml")
26
+ return soup
27
+
28
+ def get_reviews(url):
29
+ soup = get_soup(url)
30
+ if soup is None:
31
+ return "Error in getting soup"
32
+ review_elements = soup.select("div.review")
33
+
34
+ scraped_reviews = []
35
+
36
+ for review in review_elements:
37
+ r_author_element = review.select_one("span.a-profile-name")
38
+ r_author = r_author_element.text if r_author_element else None
39
+
40
+ r_rating_element = review.select_one("i.review-rating")
41
+ r_rating = r_rating_element.text.replace("out of 5 stars", "") if r_rating_element else None
42
+
43
+ r_title_element = review.select_one("a.review-title")
44
+ r_title_span_element = r_title_element.select_one("span:not([class])") if r_title_element else None
45
+ r_title = r_title_span_element.text if r_title_span_element else None
46
+
47
+ r_content_element = review.select_one("span.review-text")
48
+ r_content = r_content_element.text if r_content_element else None
49
+
50
+ r_date_element = review.select_one("span.review-date")
51
+ r_date = r_date_element.text if r_date_element else None
52
+
53
+ r_verified_element = review.select_one("span.a-size-mini")
54
+ r_verified = r_verified_element.text if r_verified_element else None
55
+
56
+ r_image_element = review.select_one("img.review-image-tile")
57
+ r_image = r_image_element.attrs["src"] if r_image_element else None
58
+
59
+ r = {
60
+ "author": r_author,
61
+ "rating": r_rating,
62
+ "title": r_title,
63
+ "content": r_content,
64
+ "date": r_date,
65
+ "verified": r_verified,
66
+ "image_url": r_image
67
+ }
68
+
69
+ scraped_reviews.append(r)
70
+
71
+ df = pd.DataFrame(data=scraped_reviews)
72
+ # sort by rating
73
+ df = df.sort_values(by=['rating'], ascending=False)
74
+ data = df['content'].to_dict()
75
+ data['Number of reviews'] = df.shape[0]
76
+ with open("reviews.json", "w") as f:
77
+ f.write(json.dumps(data))
78
+ return "success"
79
+ # df.to_csv("reviews.csv")
80
+
81
+