Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
import spacy
|
4 |
+
import yake
|
5 |
+
|
6 |
+
# Initialize Google Gemini AI
|
7 |
+
genai.configure(api_key="AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI")
|
8 |
+
|
9 |
+
# Load NLP Model
|
10 |
+
nlp = spacy.load("en_core_web_sm")
|
11 |
+
|
12 |
+
def analyze_text(text):
|
13 |
+
"""Perform AI-driven text analysis."""
|
14 |
+
if not text:
|
15 |
+
return "Please enter some text."
|
16 |
+
|
17 |
+
# Summarization using Gemini AI
|
18 |
+
prompt = f"Summarize this text:\n{text}"
|
19 |
+
response = genai.generate_text(prompt)
|
20 |
+
summary = response.text.strip() if response.text else "Error in summarization."
|
21 |
+
|
22 |
+
# Sentiment Analysis
|
23 |
+
sentiment = "Positive" if "good" in text.lower() else "Negative" # Basic example
|
24 |
+
|
25 |
+
# Keyword Extraction
|
26 |
+
kw_extractor = yake.KeywordExtractor()
|
27 |
+
keywords = [kw[0] for kw in kw_extractor.extract_keywords(text)[:5]]
|
28 |
+
|
29 |
+
# Named Entity Recognition (NER)
|
30 |
+
doc = nlp(text)
|
31 |
+
entities = {ent.text: ent.label_ for ent in doc.ents}
|
32 |
+
|
33 |
+
# AI-Generated Report
|
34 |
+
report = f"""
|
35 |
+
**Summary:** {summary}
|
36 |
+
**Sentiment:** {sentiment}
|
37 |
+
**Keywords:** {', '.join(keywords)}
|
38 |
+
**Entities:** {entities if entities else 'None'}
|
39 |
+
"""
|
40 |
+
|
41 |
+
return report
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown("# AI-Powered Text & File Analyzer 🚀")
|
46 |
+
input_text = gr.Textbox(label="Enter Text or Upload .txt File")
|
47 |
+
file_input = gr.File(label="Upload .txt File", file_types=[".txt"])
|
48 |
+
analyze_button = gr.Button("Analyze")
|
49 |
+
output = gr.Markdown()
|
50 |
+
|
51 |
+
def process_input(text, file):
|
52 |
+
"""Process text from input or file."""
|
53 |
+
if file:
|
54 |
+
with open(file.name, "r") as f:
|
55 |
+
text = f.read()
|
56 |
+
return analyze_text(text)
|
57 |
+
|
58 |
+
analyze_button.click(process_input, inputs=[input_text, file_input], outputs=output)
|
59 |
+
|
60 |
+
demo.launch()
|