Spaces:
Runtime error
Runtime error
Download app.py from yk07577/Assignment7: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/spaces/yk07577/Assignment7/resolve/main/app.py
- Command line
-
hf download hf://spaces/yk07577/Assignment7/app.py
-
curl -L -o app.py https://huggingface.co/spaces/yk07577/Assignment7/resolve/main/app.py
2.39 kB
| import gradio as gr | |
| from transformers import pipeline | |
| from sklearn.datasets import make_classification | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.svm import SVC | |
| from sklearn.metrics import accuracy_score | |
| # Step 1: Sentiment Analysis Pipeline | |
| sentiment_analyzer = pipeline("sentiment-analysis") | |
| def analyze_sentiment(text): | |
| result = sentiment_analyzer(text) | |
| return result[0] # Return the result (positive/negative) | |
| # Step 2: Machine Learning Model Suggestion and Classification | |
| def suggest_ml_models(dataset): | |
| # You can add logic to select appropriate models based on the dataset | |
| models = { | |
| "Random Forest": RandomForestClassifier(), | |
| "Logistic Regression": LogisticRegression(), | |
| "Support Vector Classifier": SVC(), | |
| } | |
| # Assuming dataset is in the form (X, y) | |
| X_train, X_test, y_train, y_test = train_test_split(dataset[0], dataset[1], test_size=0.3, random_state=42) | |
| model_accuracies = {} | |
| for model_name, model in models.items(): | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) | |
| model_accuracies[model_name] = accuracy | |
| return model_accuracies | |
| # Step 3: Create a synthetic dataset for classification | |
| def create_synthetic_dataset(): | |
| # Creating a synthetic classification dataset | |
| X, y = make_classification(n_samples=1000, n_features=20, random_state=42) | |
| return X, y | |
| # Combine both functions in Gradio for user interaction | |
| def analyze_and_suggest(text): | |
| # Sentiment analysis | |
| sentiment_result = analyze_sentiment(text) | |
| # Generate synthetic dataset | |
| dataset = create_synthetic_dataset() | |
| # Model suggestion for classification | |
| model_accuracies = suggest_ml_models(dataset) | |
| # Return both the sentiment analysis result and model suggestions | |
| return sentiment_result, model_accuracies | |
| # Gradio interface for user interaction | |
| interface = gr.Interface( | |
| fn=analyze_and_suggest, | |
| inputs=gr.Textbox(label="Enter Text for Sentiment Analysis"), | |
| outputs=[gr.JSON(label="Sentiment Analysis Result"), gr.JSON(label="Model Suggestions and Accuracies")], | |
| title="Sentiment Analysis and ML Model Suggestion" | |
| ) | |
| # Launch Gradio Interface | |
| interface.launch() | |