Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 6 |
|
| 7 |
-
# Define a function that will analyze sentiment
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
result = sentiment_analyzer(text)
|
| 10 |
-
return result[0]
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Launch
|
| 21 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from sklearn.datasets import make_classification
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.svm import SVC
|
| 8 |
+
from sklearn.metrics import accuracy_score
|
| 9 |
|
| 10 |
+
# Step 1: Sentiment Analysis Pipeline
|
| 11 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 12 |
|
|
|
|
| 13 |
def analyze_sentiment(text):
|
| 14 |
result = sentiment_analyzer(text)
|
| 15 |
+
return result[0] # Return the result (positive/negative)
|
| 16 |
+
|
| 17 |
+
# Step 2: Machine Learning Model Suggestion and Classification
|
| 18 |
+
def suggest_ml_models(dataset):
|
| 19 |
+
# You can add logic to select appropriate models based on the dataset
|
| 20 |
+
models = {
|
| 21 |
+
"Random Forest": RandomForestClassifier(),
|
| 22 |
+
"Logistic Regression": LogisticRegression(),
|
| 23 |
+
"Support Vector Classifier": SVC(),
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Assuming dataset is in the form (X, y)
|
| 27 |
+
X_train, X_test, y_train, y_test = train_test_split(dataset[0], dataset[1], test_size=0.3, random_state=42)
|
| 28 |
+
|
| 29 |
+
model_accuracies = {}
|
| 30 |
+
for model_name, model in models.items():
|
| 31 |
+
model.fit(X_train, y_train)
|
| 32 |
+
y_pred = model.predict(X_test)
|
| 33 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 34 |
+
model_accuracies[model_name] = accuracy
|
| 35 |
+
|
| 36 |
+
return model_accuracies
|
| 37 |
+
|
| 38 |
+
# Step 3: Create a synthetic dataset for classification
|
| 39 |
+
def create_synthetic_dataset():
|
| 40 |
+
# Creating a synthetic classification dataset
|
| 41 |
+
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
|
| 42 |
+
return X, y
|
| 43 |
+
|
| 44 |
+
# Combine both functions in Gradio for user interaction
|
| 45 |
+
def analyze_and_suggest(text):
|
| 46 |
+
# Sentiment analysis
|
| 47 |
+
sentiment_result = analyze_sentiment(text)
|
| 48 |
+
|
| 49 |
+
# Generate synthetic dataset
|
| 50 |
+
dataset = create_synthetic_dataset()
|
| 51 |
+
|
| 52 |
+
# Model suggestion for classification
|
| 53 |
+
model_accuracies = suggest_ml_models(dataset)
|
| 54 |
+
|
| 55 |
+
# Return both the sentiment analysis result and model suggestions
|
| 56 |
+
return sentiment_result, model_accuracies
|
| 57 |
+
|
| 58 |
+
# Gradio interface for user interaction
|
| 59 |
+
interface = gr.Interface(
|
| 60 |
+
fn=analyze_and_suggest,
|
| 61 |
+
inputs=gr.Textbox(label="Enter Text for Sentiment Analysis"),
|
| 62 |
+
outputs=[gr.JSON(label="Sentiment Analysis Result"), gr.JSON(label="Model Suggestions and Accuracies")],
|
| 63 |
+
title="Sentiment Analysis and ML Model Suggestion"
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Launch Gradio Interface
|
| 67 |
+
interface.launch()
|
| 68 |
+
|