Ujeshhh commited on
Commit
734c06e
·
verified ·
1 Parent(s): b5ab401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -1,13 +1,20 @@
 
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."""
@@ -15,12 +22,15 @@ def analyze_text(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()
@@ -32,9 +42,9 @@ def analyze_text(text):
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
 
@@ -43,15 +53,16 @@ def analyze_text(text):
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
 
 
1
+ import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
  import spacy
5
  import yake
6
+ import subprocess
7
 
8
+ # Ensure spaCy model is downloaded dynamically
9
+ MODEL_NAME = "en_core_web_sm"
10
+ try:
11
+ nlp = spacy.load(MODEL_NAME)
12
+ except OSError:
13
+ subprocess.run(["python", "-m", "spacy", "download", MODEL_NAME])
14
+ nlp = spacy.load(MODEL_NAME)
15
 
16
+ # Configure Google Gemini AI
17
+ genai.configure(api_key=os.getenv("AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI")) # Use environment variable for security
18
 
19
  def analyze_text(text):
20
  """Perform AI-driven text analysis."""
 
22
  return "Please enter some text."
23
 
24
  # Summarization using Gemini AI
25
+ try:
26
+ prompt = f"Summarize this text:\n{text}"
27
+ response = genai.generate_text(prompt)
28
+ summary = response.text.strip() if response.text else "Error in summarization."
29
+ except Exception as e:
30
+ summary = f"Summarization failed: {str(e)}"
31
 
32
+ # Basic Sentiment Analysis (Improve with ML if needed)
33
+ sentiment = "Positive" if "good" in text.lower() else "Negative"
34
 
35
  # Keyword Extraction
36
  kw_extractor = yake.KeywordExtractor()
 
42
 
43
  # AI-Generated Report
44
  report = f"""
45
+ **Summary:** {summary}
46
+ **Sentiment:** {sentiment}
47
+ **Keywords:** {', '.join(keywords)}
48
  **Entities:** {entities if entities else 'None'}
49
  """
50
 
 
53
  # Gradio Interface
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# AI-Powered Text & File Analyzer 🚀")
56
+
57
+ input_text = gr.Textbox(label="Enter Text")
58
  file_input = gr.File(label="Upload .txt File", file_types=[".txt"])
59
  analyze_button = gr.Button("Analyze")
60
  output = gr.Markdown()
61
+
62
  def process_input(text, file):
63
  """Process text from input or file."""
64
  if file:
65
+ with open(file.name, "r", encoding="utf-8") as f:
66
  text = f.read()
67
  return analyze_text(text)
68