AndrewNanu-app's picture
Create app.py
1c00326 verified
raw
history blame contribute delete
No virus
3.82 kB
import json
import csv
import os
from transformers import pipeline
from datasets import load_dataset, Dataset
import gradio as gr
# Load the zero-shot-classification model
classifier = pipeline(model="facebook/bart-large-mnli")
# Define the candidate labels for sentiment classification
candidate_labels = ["neutral", "skeptical", "intrigued", "amazed", "concerned", "fearful", "curious", "horrified", "paralyzed"]
def preprocess_text(text):
# Perform text preprocessing steps
text = text.lower()
# Add more preprocessing steps as needed
return text
def classify_sentiment(input_data):
# Convert input data to regular text
if isinstance(input_data, dict):
# JSON input
text = json.dumps(input_data)
elif isinstance(input_data, list):
# CSV input
text = "\n".join([",".join(row) for row in input_data])
else:
# Regular text input
text = input_data
# Preprocess the text
text = preprocess_text(text)
# Classify the sentiment of the input
result = classifier(text.strip(), candidate_labels=candidate_labels)
# Create the result object
sentiment_result = {
"Input": text,
"Sentiment": result['labels'][0],
"Score": result['scores'][0]
}
return sentiment_result
def load_huggingface_dataset(dataset_path):
# Load the dataset from Hugging Face
dataset = load_dataset(dataset_path)
# Extract the text data from the dataset
text_data = dataset["train"]["text"]
return text_data
def save_huggingface_dataset(data, dataset_path):
# Create a Dataset object from the data
dataset = Dataset.from_dict({"text": data})
# Save the dataset to Hugging Face
dataset.push_to_hub(dataset_path)
# Create a Gradio interface
iface = gr.Interface(
fn=classify_sentiment,
inputs=gr.Textbox(label="Input Data", lines=10, placeholder="Paste your input data here (text, CSV, or JSON)"),
outputs=gr.JSON(label="Results"),
title="UAP Report Sentiment Analysis",
description="Paste your input data (text, CSV, or JSON) and get the sentiment analysis result.",
examples=[
[
'NUFORC Sighting 181448\nOccurred: 2002-09-17 19:10 Local\nUFO from the big dipper\nAt the older barracks in the parking lot 2h after dinner. I and 6 other soldiers in an Army NG Unit were enjoying the night, i happened to look up and saw the Big Dipper, but..one of the stars - the lower left one, was much bigger and brighter than others. "A Nova?" I uttered, believing it was one until it moved, east and dropped in altitude. "Hey guys LOOK!" I called. Our Lt and others saw it too. The object hovered near some water towers about 300 meters east of us. it then slowly approached us. "HIDE" The Lt. shouted. I and a female soldier hid under a picnic table as the others ducked into the barracks.\nThe soldier with me was near panicked. I had to calm her as the thing kept coming. I heard and read of these things abducting people such as Travis Walton and i did not want that. Then the object shone a very bright light like a searchlight at us. It lit the entire area up. I knew the thing was looking for us.\nIt kept coming, silently, slowly and stopped about 150M off. The power in the area went out, even battery-powered devices were dead, radios, watches, flashlights. I could see the thing was disc shaped, about 100 ft dia. quite large.\nThen the light went out, and the craft lifted up about 150\' up, sped off South over some mountains 2miles away silently. Power came back on and we gathered again.\nThe Lt. told us to be quiet about it. The following days the MPs went to that barracks and combed it for evidence as the UFO was seen miles away.'
]
]
)
# Launch the Gradio interface
iface.launch()