ravi6389 commited on
Commit
e86c3bb
·
verified ·
1 Parent(s): 9922870

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -124
app.py CHANGED
@@ -1,125 +1,126 @@
1
- import gradio as gr
2
- import selenium
3
- from selenium import webdriver
4
- from selenium.webdriver.common.keys import Keys
5
- from selenium.common.exceptions import NoSuchElementException
6
- from selenium.webdriver import Chrome, ChromeOptions
7
- import time
8
- import pandas as pd
9
- from selenium.webdriver.support import expected_conditions as EC
10
- from selenium.webdriver.common.by import By
11
- from selenium.webdriver.support.ui import WebDriverWait
12
- from time import sleep
13
- from webdriver_manager.chrome import ChromeDriverManager
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
- global name
23
- global password
24
- global topic
25
-
26
- def login_and_work(username_text, password_text, topic_text):
27
- #options = webdriver.ChromeOptions()
28
- #options.add_argument('--headless')
29
- #options.add_argument('--no-sandbox')
30
- # options.add_argument('--disable-dev-shm-usage')
31
- # options.use_chromium = True
32
- driver = webdriver.Chrome(ChromeDriverManager().install())
33
- driver.get('https://twitter.com/i/flow/login')
34
- time.sleep(5)
35
- username = driver.find_element("xpath",'//input[@name="text"]')
36
- username.send_keys(username_text)
37
- driver.find_element("xpath",'(//*[@role="button"])[4]').click() # in prevoius block
38
-
39
- try:
40
- time.sleep(10)
41
- y = driver.find_element("xpath",'//h1//span')
42
- if(y.text =='Enter your phone number or username'):
43
- username2 = driver.find_element("xpath",'//input[@name="text"]')
44
- username2.send_keys(username_text)
45
- driver.find_element("xpath",'(//*[@role="button"])[2]').click()
46
- except:
47
-
48
- x = 10
49
-
50
- password = driver.find_element("xpath",'//input[@name="password"]')
51
- password.send_keys(password_text)
52
- driver.find_element("xpath",'(//*[@role = "button"])[4]').click()
53
-
54
-
55
- try:
56
- driver.maximize_window()
57
- time.sleep(5)
58
- driver.maximize_window()
59
- search = driver.find_element("xpath",'//input[@placeholder="Search"]')
60
- search.send_keys(Keys.CONTROL + "a")
61
- search.send_keys(Keys.DELETE)
62
- search.send_keys(topic_text)
63
- search.send_keys(Keys.RETURN)
64
-
65
- df = pd.DataFrame()
66
- df['Name']=''
67
- df['Tweet'] = ''
68
- #df['Sentiment'] = ''
69
- tweet_count = 6
70
- i = 1
71
- element = driver.find_element("xpath","//body")
72
- time.sleep(5)
73
- while True and i <= tweet_count:
74
-
75
- try:
76
- tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
77
- df.loc[i, 'Name'] = tweet_name.text
78
-
79
- tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
80
- if(tweet_div):
81
- #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
82
- df.loc[i, 'Tweet'] = tweet_div.text
83
- i += 1
84
- element.send_keys(Keys.PAGE_DOWN)
85
- time.sleep(5)
86
- except:
87
- time.sleep(5)
88
- tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
89
- df.loc[i, 'Name'] = tweet_name.text
90
-
91
- tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
92
- if(tweet_div):
93
- #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
94
- df.loc[i, 'Tweet'] = tweet_div.text
95
- i += 1
96
- element.send_keys(Keys.PAGE_DOWN)
97
- time.sleep(5)
98
-
99
-
100
- #model = tweetnlp.load_model('sentiment', multilingual=True) # Or `model = tweetnlp.Sentiment()`
101
-
102
- # for i in range(1,len(df)+1):
103
- # print("text is...", df.loc[i,'Tweet'])
104
-
105
-
106
- # #y = sentiment_pipeline(df.loc[i, 'Tweet'])
107
- # #print('label is..', y[0]['label'])
108
- # print('model sentiment is..', model.sentiment(df.loc[i, 'Tweet']))
109
- # df.loc[i, 'Sentiment'] = str(model.sentiment(df.loc[i, 'Tweet']))
110
-
111
- return df
112
-
113
- except Exception as e:
114
- x = 10
115
-
116
- with gr.Blocks() as demo:
117
-
118
- name = gr.Textbox(label="Username")
119
- password = gr.Textbox(label="Password", type = 'password')
120
- topic= gr.Textbox(label="Topic")
121
-
122
- btn = gr.Button(value="Submit")
123
- btn.click(login_and_work, inputs=[name, password, topic], outputs= gr.Dataframe(headers=['Name', 'Tweet']))
124
-
 
125
  demo.launch(share=True)
 
1
+ import gradio as gr
2
+ import selenium
3
+ from selenium import webdriver
4
+ from selenium.webdriver.common.keys import Keys
5
+ from selenium.common.exceptions import NoSuchElementException
6
+ from selenium.webdriver import Chrome, ChromeOptions
7
+ import time
8
+ import pandas as pd
9
+ from selenium.webdriver.support import expected_conditions as EC
10
+ from selenium.webdriver.common.by import By
11
+ from selenium.webdriver.support.ui import WebDriverWait
12
+ from time import sleep
13
+ from webdriver_manager.chrome import ChromeDriverManager
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ global name
23
+ global password
24
+ global topic
25
+
26
+ def login_and_work(username_text, password_text, topic_text):
27
+ #options = webdriver.ChromeOptions()
28
+ #options.add_argument('--headless')
29
+ #options.add_argument('--no-sandbox')
30
+ # options.add_argument('--disable-dev-shm-usage')
31
+ # options.use_chromium = True
32
+ options = webdriver.ChromeOptions()
33
+ driver = webdriver.Chrome(options = options)
34
+ driver.get('https://twitter.com/i/flow/login')
35
+ time.sleep(5)
36
+ username = driver.find_element("xpath",'//input[@name="text"]')
37
+ username.send_keys(username_text)
38
+ driver.find_element("xpath",'(//*[@role="button"])[4]').click() # in prevoius block
39
+
40
+ try:
41
+ time.sleep(10)
42
+ y = driver.find_element("xpath",'//h1//span')
43
+ if(y.text =='Enter your phone number or username'):
44
+ username2 = driver.find_element("xpath",'//input[@name="text"]')
45
+ username2.send_keys(username_text)
46
+ driver.find_element("xpath",'(//*[@role="button"])[2]').click()
47
+ except:
48
+
49
+ x = 10
50
+
51
+ password = driver.find_element("xpath",'//input[@name="password"]')
52
+ password.send_keys(password_text)
53
+ driver.find_element("xpath",'(//*[@role = "button"])[4]').click()
54
+
55
+
56
+ try:
57
+ driver.maximize_window()
58
+ time.sleep(5)
59
+ driver.maximize_window()
60
+ search = driver.find_element("xpath",'//input[@placeholder="Search"]')
61
+ search.send_keys(Keys.CONTROL + "a")
62
+ search.send_keys(Keys.DELETE)
63
+ search.send_keys(topic_text)
64
+ search.send_keys(Keys.RETURN)
65
+
66
+ df = pd.DataFrame()
67
+ df['Name']=''
68
+ df['Tweet'] = ''
69
+ #df['Sentiment'] = ''
70
+ tweet_count = 6
71
+ i = 1
72
+ element = driver.find_element("xpath","//body")
73
+ time.sleep(5)
74
+ while True and i <= tweet_count:
75
+
76
+ try:
77
+ tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
78
+ df.loc[i, 'Name'] = tweet_name.text
79
+
80
+ tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
81
+ if(tweet_div):
82
+ #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
83
+ df.loc[i, 'Tweet'] = tweet_div.text
84
+ i += 1
85
+ element.send_keys(Keys.PAGE_DOWN)
86
+ time.sleep(5)
87
+ except:
88
+ time.sleep(5)
89
+ tweet_name = driver.find_element("xpath",f'(//*[@data-testid="User-Name"])[{i}]')
90
+ df.loc[i, 'Name'] = tweet_name.text
91
+
92
+ tweet_div = driver.find_element("xpath",f'(//*[@data-testid="tweetText"])[{i}]')
93
+ if(tweet_div):
94
+ #print(driver.find_element("xpath",'(//*[@data-testid="tweetText"])[1]').getText())
95
+ df.loc[i, 'Tweet'] = tweet_div.text
96
+ i += 1
97
+ element.send_keys(Keys.PAGE_DOWN)
98
+ time.sleep(5)
99
+
100
+
101
+ #model = tweetnlp.load_model('sentiment', multilingual=True) # Or `model = tweetnlp.Sentiment()`
102
+
103
+ # for i in range(1,len(df)+1):
104
+ # print("text is...", df.loc[i,'Tweet'])
105
+
106
+
107
+ # #y = sentiment_pipeline(df.loc[i, 'Tweet'])
108
+ # #print('label is..', y[0]['label'])
109
+ # print('model sentiment is..', model.sentiment(df.loc[i, 'Tweet']))
110
+ # df.loc[i, 'Sentiment'] = str(model.sentiment(df.loc[i, 'Tweet']))
111
+
112
+ return df
113
+
114
+ except Exception as e:
115
+ x = 10
116
+
117
+ with gr.Blocks() as demo:
118
+
119
+ name = gr.Textbox(label="Username")
120
+ password = gr.Textbox(label="Password", type = 'password')
121
+ topic= gr.Textbox(label="Topic")
122
+
123
+ btn = gr.Button(value="Submit")
124
+ btn.click(login_and_work, inputs=[name, password, topic], outputs= gr.Dataframe(headers=['Name', 'Tweet']))
125
+
126
  demo.launch(share=True)