Spaces:
Sleeping
Sleeping
Add Telegram integration and SQLite database for sentiment analysis results
Browse files- .env +2 -0
- app.py +55 -0
- requirements.txt +2 -1
.env
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
TELEGRAM_BOT_TOKEN=<your-telegram-bot-token>
|
| 2 |
+
TELEGRAM_CHAT_ID=<your-chat-id>
|
app.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the Sentiment Analysis pipeline...
|
| 5 |
classifier = pipeline(
|
|
@@ -7,6 +14,38 @@ classifier = pipeline(
|
|
| 7 |
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 8 |
)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Define the prediction function...
|
| 11 |
def sentiment_predictor(text):
|
| 12 |
if not text:
|
|
@@ -17,6 +56,22 @@ def sentiment_predictor(text):
|
|
| 17 |
score = result['score']
|
| 18 |
|
| 19 |
output_text = f"Predicted Sentiment: **{label}**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
return output_text, score
|
| 22 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import sqlite3
|
| 4 |
+
import requests
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
# Load the Sentiment Analysis pipeline...
|
| 12 |
classifier = pipeline(
|
|
|
|
| 14 |
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Initialize SQLite database connection
|
| 18 |
+
conn = sqlite3.connect("sentiment_analysis.db")
|
| 19 |
+
cursor = conn.cursor()
|
| 20 |
+
|
| 21 |
+
# Create a table to store input and output if it doesn't exist
|
| 22 |
+
cursor.execute("""
|
| 23 |
+
CREATE TABLE IF NOT EXISTS SentimentAnalysis (
|
| 24 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 25 |
+
input_text TEXT NOT NULL,
|
| 26 |
+
output_text TEXT NOT NULL,
|
| 27 |
+
confidence_score REAL NOT NULL
|
| 28 |
+
)
|
| 29 |
+
""")
|
| 30 |
+
conn.commit()
|
| 31 |
+
|
| 32 |
+
# Define your Telegram bot token and chat ID
|
| 33 |
+
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 34 |
+
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
| 35 |
+
|
| 36 |
+
# Function to send messages to the Telegram bot
|
| 37 |
+
def send_to_telegram(message):
|
| 38 |
+
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
| 39 |
+
payload = {
|
| 40 |
+
"chat_id": TELEGRAM_CHAT_ID,
|
| 41 |
+
"text": message,
|
| 42 |
+
"parse_mode": "Markdown"
|
| 43 |
+
}
|
| 44 |
+
try:
|
| 45 |
+
requests.post(url, data=payload)
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
print(f"Failed to send message to Telegram: {e}")
|
| 48 |
+
|
| 49 |
# Define the prediction function...
|
| 50 |
def sentiment_predictor(text):
|
| 51 |
if not text:
|
|
|
|
| 56 |
score = result['score']
|
| 57 |
|
| 58 |
output_text = f"Predicted Sentiment: **{label}**"
|
| 59 |
+
|
| 60 |
+
# Save input and output to the database
|
| 61 |
+
cursor.execute(
|
| 62 |
+
"INSERT INTO SentimentAnalysis (input_text, output_text, confidence_score) VALUES (?, ?, ?)",
|
| 63 |
+
(text, output_text, score)
|
| 64 |
+
)
|
| 65 |
+
conn.commit()
|
| 66 |
+
|
| 67 |
+
# Send input and output to the Telegram bot
|
| 68 |
+
message = (
|
| 69 |
+
f"*New Sentiment Analysis Result:*\n"
|
| 70 |
+
f"*Input:* {text}\n"
|
| 71 |
+
f"*Output:* {output_text}\n"
|
| 72 |
+
f"*Confidence Score:* {score:.2f}"
|
| 73 |
+
)
|
| 74 |
+
send_to_telegram(message)
|
| 75 |
|
| 76 |
return output_text, score
|
| 77 |
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
transformers
|
| 3 |
-
torch
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
transformers
|
| 3 |
+
torch
|
| 4 |
+
python-dotenv
|