| import gradio as gr |
| from transformers import pipeline |
| import re |
| |
| |
| sentiment_model = pipeline("sentiment-analysis", model="Anjanie/roberta-sentiment") |
| toxicity_model = pipeline("text-classification", model="Anjanie/distilbert-base-uncased-toxicity") |
| misinfo_model = pipeline("text-classification", model="Anjanie/bert-base-uncased-misinformation") |
| ner_model = pipeline("ner", model="Anjanie/bert-base-cased-ner", grouped_entities=True) |
| |
| def analyze_text(text): |
| sentiment = sentiment_model(text) |
| toxicity = toxicity_model(text) |
| misinfo = misinfo_model(text) |
| ner_results = ner_model(text) |
| entities = [ |
| { |
| "entity": e.get("entity_group", e.get("entity", "")), |
| "word": e.get("word", "") |
| } |
| for e in ner_results |
| ] |
| |
| phones = re.findall(r"\b\d{9,13}\b", text) |
| emails = re.findall(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", text) |
| |
| |
| return { |
| "sentiment": sentiment[0]["label"], |
| "toxicity": toxicity[0]["label"], |
| "misinformation": misinfo[0]["label"], |
| "entities": entities, |
| "phones": list(set(phones)), |
| "emails": list(set(emails)), |
| "sentiment_model_output": sentiment, |
| "toxicity_model_output": toxicity, |
| "misinformation_model_output": misinfo, |
| "ner_model_output": ner_results, |
| } |
| |
| iface = gr.Interface( |
| fn=analyze_text, |
| inputs=gr.Textbox(lines=3, placeholder="Enter text here..."), |
| outputs=gr.JSON(), |
| title="Cyberhunk Text Analyzer", |
| description="Sentiment, Toxicity, Misinformation, and Named Entity Recognition (NER) analysis" |
| ) |
| |
| if __name__ == "__main__": |
| iface.launch(server_name="0.0.0.0", server_port=7860, share=True) |
| |