Spaces:
Sleeping
Sleeping
arminapr commited on
Commit ·
0535dd8
1
Parent(s): 1fc76db
update all
Browse files- app.py +4 -7
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -14,14 +14,14 @@ tokenizer = BertTokenizerFast.from_pretrained(tokenizer_save_path)
|
|
| 14 |
|
| 15 |
sentiment_mapping = {0: 'neutral', 1: 'negative', 2: 'positive'}
|
| 16 |
|
| 17 |
-
#
|
| 18 |
def predict_sentiment(text):
|
| 19 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
|
| 20 |
outputs = model(**inputs)
|
| 21 |
predictions = torch.argmax(outputs.logits, dim=-1).cpu().numpy()
|
| 22 |
return sentiment_mapping[predictions[0]]
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def scrape_and_predict():
|
| 26 |
url = "https://finance.yahoo.com"
|
| 27 |
response = requests.get(url)
|
|
@@ -37,15 +37,13 @@ def scrape_and_predict():
|
|
| 37 |
|
| 38 |
return headlines
|
| 39 |
|
| 40 |
-
# Gradio interface for sentiment prediction
|
| 41 |
def sentiment_interface(text):
|
| 42 |
return predict_sentiment(text)
|
| 43 |
|
| 44 |
-
# Gradio interface for scraping headlines and predicting their sentiment
|
| 45 |
def scrape_interface():
|
| 46 |
return scrape_and_predict()
|
| 47 |
|
| 48 |
-
#
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
gr.Markdown("# Sentiment Analysis and News Scraping")
|
| 51 |
|
|
@@ -60,6 +58,5 @@ with gr.Blocks() as demo:
|
|
| 60 |
headlines_output = gr.JSON(label="Headlines and Sentiment")
|
| 61 |
scrape_button.click(scrape_interface, outputs=headlines_output)
|
| 62 |
|
| 63 |
-
# Launch the Gradio app
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
demo.launch()
|
|
|
|
| 14 |
|
| 15 |
sentiment_mapping = {0: 'neutral', 1: 'negative', 2: 'positive'}
|
| 16 |
|
| 17 |
+
# function to predict the sentiment of the text
|
| 18 |
def predict_sentiment(text):
|
| 19 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
|
| 20 |
outputs = model(**inputs)
|
| 21 |
predictions = torch.argmax(outputs.logits, dim=-1).cpu().numpy()
|
| 22 |
return sentiment_mapping[predictions[0]]
|
| 23 |
|
| 24 |
+
# function to scrape headlines and predict their sentiment
|
| 25 |
def scrape_and_predict():
|
| 26 |
url = "https://finance.yahoo.com"
|
| 27 |
response = requests.get(url)
|
|
|
|
| 37 |
|
| 38 |
return headlines
|
| 39 |
|
|
|
|
| 40 |
def sentiment_interface(text):
|
| 41 |
return predict_sentiment(text)
|
| 42 |
|
|
|
|
| 43 |
def scrape_interface():
|
| 44 |
return scrape_and_predict()
|
| 45 |
|
| 46 |
+
# gradio app
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("# Sentiment Analysis and News Scraping")
|
| 49 |
|
|
|
|
| 58 |
headlines_output = gr.JSON(label="Headlines and Sentiment")
|
| 59 |
scrape_button.click(scrape_interface, outputs=headlines_output)
|
| 60 |
|
|
|
|
| 61 |
if __name__ == "__main__":
|
| 62 |
+
demo.launch(share=True)
|
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ transformers==4.31.0
|
|
| 5 |
beautifulsoup4==4.12.2
|
| 6 |
requests==2.31.0
|
| 7 |
gradio==3.23.0
|
| 8 |
-
safetensors==0.3.2
|
|
|
|
|
|
| 5 |
beautifulsoup4==4.12.2
|
| 6 |
requests==2.31.0
|
| 7 |
gradio==3.23.0
|
| 8 |
+
safetensors==0.3.2
|
| 9 |
+
numpy<2
|