import gradio as gr import google.generativeai as genai import spacy import yake # Initialize Google Gemini AI genai.configure(api_key="AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI") # Load NLP Model nlp = spacy.load("en_core_web_sm") def analyze_text(text): """Perform AI-driven text analysis.""" if not text: return "Please enter some text." # Summarization using Gemini AI prompt = f"Summarize this text:\n{text}" response = genai.generate_text(prompt) summary = response.text.strip() if response.text else "Error in summarization." # Sentiment Analysis sentiment = "Positive" if "good" in text.lower() else "Negative" # Basic example # Keyword Extraction kw_extractor = yake.KeywordExtractor() keywords = [kw[0] for kw in kw_extractor.extract_keywords(text)[:5]] # Named Entity Recognition (NER) doc = nlp(text) entities = {ent.text: ent.label_ for ent in doc.ents} # AI-Generated Report report = f""" **Summary:** {summary} **Sentiment:** {sentiment} **Keywords:** {', '.join(keywords)} **Entities:** {entities if entities else 'None'} """ return report # Gradio Interface with gr.Blocks() as demo: gr.Markdown("# AI-Powered Text & File Analyzer 🚀") input_text = gr.Textbox(label="Enter Text or Upload .txt File") file_input = gr.File(label="Upload .txt File", file_types=[".txt"]) analyze_button = gr.Button("Analyze") output = gr.Markdown() def process_input(text, file): """Process text from input or file.""" if file: with open(file.name, "r") as f: text = f.read() return analyze_text(text) analyze_button.click(process_input, inputs=[input_text, file_input], outputs=output) demo.launch()