BoojithDharshan commited on
Commit
80c91b5
Β·
verified Β·
1 Parent(s): 7682b1e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+ import textstat # For readability and complexity score
5
+
6
+ # Ensure the API key is securely set
7
+ GOOGLE_API_KEY = os.getenv("AIzaSyBxv9sXG1dyl4ugOOMmMU9QgyMYSvVKg5s") # Load API key from environment variable
8
+ genai.configure(api_key=GOOGLE_API_KEY)
9
+
10
+ # Load the Gemini Model
11
+ model = genai.GenerativeModel(model_name="models/gemini-2.0-pro") # Upgraded model
12
+
13
+
14
+ def analyze_input(text, file):
15
+ try:
16
+ if file is not None:
17
+ with open(file, "r", encoding="utf-8") as f:
18
+ text = f.read()
19
+ elif not text.strip():
20
+ return "⚠️ Error: Please enter text or upload a file.", "", "", ""
21
+
22
+ text = text[:3000] # Increased input text limit
23
+ prompt = f"Analyze, summarize, and extract key insights from this document:\n\n{text}"
24
+ response = model.generate_content([prompt], stream=False)
25
+ result = response.text if response else "No response from AI."
26
+ word_count = len(text.split())
27
+
28
+ # Additional insights
29
+ insight_prompt = f"Provide key topics, sentiment analysis, and readability score for this document:\n\n{text}"
30
+ insight_response = model.generate_content([insight_prompt], stream=False)
31
+ insights = insight_response.text if insight_response else "No insights available."
32
+
33
+ # Readability score
34
+ readability_score = textstat.flesch_reading_ease(text)
35
+ grammar_check = f"Readability Score: {readability_score:.2f} (Higher is easier to read)"
36
+
37
+ return result, f"πŸ“Š Word Count: {word_count}", insights, grammar_check
38
+ except Exception as e:
39
+ return f"⚠️ Error: {str(e)}", "", "", ""
40
+
41
+
42
+ def clear_inputs():
43
+ return "", None, "", "", "", ""
44
+
45
+
46
+ def generate_downloadable_file(text):
47
+ if text.strip():
48
+ file_path = "analysis_result.txt"
49
+ with open(file_path, "w", encoding="utf-8") as f:
50
+ f.write(text)
51
+ return file_path
52
+ else:
53
+ return None
54
+
55
+
56
+ # Gradio UI with an enhanced layout
57
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
+ gr.Markdown("""
59
+ # πŸ“„ **AI-Powered Text & File Analyzer**
60
+ πŸš€ Upload a `.txt` file or enter text manually to get an AI-generated analysis, summary, and insights.
61
+ """)
62
+
63
+ with gr.Row():
64
+ text_input = gr.Textbox(label="✍️ Enter Text", placeholder="Type or paste your text here...", lines=6)
65
+ file_input = gr.File(label="πŸ“‚ Upload Text File (.txt)", type="filepath")
66
+
67
+ with gr.Row():
68
+ analyze_button = gr.Button("πŸ” Analyze", variant="primary")
69
+ clear_button = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
70
+
71
+ with gr.Column():
72
+ output_text = gr.Textbox(label="πŸ“ Analysis & Summary", lines=10, interactive=False)
73
+ word_count_display = gr.Textbox(label="πŸ“Š Word Count", interactive=False)
74
+ insights_display = gr.Textbox(label="πŸ”Ž AI Insights (Topics & Sentiment)", lines=5, interactive=False)
75
+ readability_display = gr.Textbox(label="πŸ“– Readability & Grammar Check", interactive=False)
76
+
77
+ with gr.Row():
78
+ download_button = gr.Button("⬇️ Download Result", variant="success", size="sm")
79
+ download_file = gr.File(label="πŸ“„ Click to Download", interactive=False)
80
+
81
+ analyze_button.click(analyze_input, inputs=[text_input, file_input], outputs=[output_text, word_count_display, insights_display, readability_display])
82
+ clear_button.click(clear_inputs, inputs=[], outputs=[text_input, file_input, output_text, word_count_display, insights_display, readability_display, download_file])
83
+ download_button.click(generate_downloadable_file, inputs=output_text, outputs=download_file)
84
+
85
+ # Launch the Gradio app
86
+ demo.launch()