Akash076's picture
Update app.py
b4a7911 verified
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
import torch.nn.functional as F
import logging
from datetime import datetime
# Load model and tokenizer
model_path = "./"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Configure logging
LOG_FILE = "user_logs.txt"
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
def log_user_input(input_text: str, source: str = "WebApp"):
log_entry = f"{datetime.now()} | Source: {source} | Input: {input_text}"
logging.info(log_entry)
def predict_sentiment(text: str) -> dict:
log_user_input(text)
inputs = tokenizer(
text,
return_tensors="pt",
padding="max_length",
truncation=True,
max_length=128,
)
with torch.no_grad():
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=-1)
sentiment_probs = {
"Neutral": round(probs[0][0].item(), 4),
"Positive": round(probs[0][1].item(), 4),
"Negative": round(probs[0][2].item(), 4),
}
return sentiment_probs
# Gradio Interface
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(
lines=2,
placeholder="Type a tweet or sentence here...",
label="Enter Text",
elem_id="input_textbox"
),
outputs=gr.JSON(label="Sentiment Probabilities"),
title="Sentiment Analysis Application",
description="Analyze text sentiment (Negative, Neutral, Positive) with probabilities.",
examples=[
"I love this product!",
"This is the worst service I've ever had.",
"The weather today is neutral, not too hot or cold.",
],
css=""" /* Styling for Input Textbox */
#input_textbox {
font-size: 16px;
padding: 15px;
border-radius: 10px;
border: 1px solid #4CAF50; /* Green border */
width: 80%;
margin: 0 auto;
font-family: 'Arial', sans-serif;
background-color: #f4fdf1; /* Light green background */
}
/* Styling for the entire Gradio container */
.gradio-container {
background-color: #f0f8ff; /* Light blue background */
border-radius: 15px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
/* Title Styling */
.gradio-title {
color: #2C3E50; /* Dark gray-blue text color */
font-family: 'Arial', sans-serif;
font-size: 28px;
font-weight: bold;
text-align: center;
}
/* Description Styling */
.gradio-description {
font-size: 16px;
font-family: 'Arial', sans-serif;
color: #34495E; /* Darker gray color */
text-align: center;
}
/* Button Styling */
.gradio-btn {
background-color: #4CAF50; /* Green button */
color: white;
border-radius: 8px;
padding: 10px;
font-size: 14px;
width: 100%;
font-family: 'Arial', sans-serif;
margin-top: 15px;
}
/* Button Hover Styling */
.gradio-btn:hover {
background-color: #45a049; /* Slightly darker green on hover */
}
/* Output JSON Styling */
.gradio-json {
font-size: 16px;
color: #333;
background-color: #eaf2f8; /* Very light blue background */
padding: 10px;
border-radius: 8px;
border: 1px solid #ddd;
}
"""
)
iface.launch(share=True)