UmaGeeth commited on
Commit
342a3e6
Β·
verified Β·
1 Parent(s): a1a744b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +76 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+
5
+ # Set Google API Key
6
+ GOOGLE_API_KEY = "AIzaSyAfR5zgLAuYaBeJUxnVjkKTII5uneNmKrk"
7
+ genai.configure(api_key=GOOGLE_API_KEY)
8
+
9
+ # Load the Gemini Model
10
+ model = genai.GenerativeModel(model_name="models/gemini-2.0-flash")
11
+
12
+ def analyze_input(text, file):
13
+ try:
14
+ if file is not None:
15
+ with open(file, "r", encoding="utf-8") as f:
16
+ text = f.read()
17
+ elif not text.strip():
18
+ return "⚠️ Error: Please enter text or upload a file.", ""
19
+
20
+ text = text[:2000] # Limit input text size
21
+ prompt = f"Analyze and summarize this document:\n\n{text}"
22
+ response = model.generate_content([prompt], stream=True) # βœ… Fix applied
23
+
24
+ # Collect streamed response
25
+ result = "".join([chunk.text for chunk in response])
26
+ word_count = len(text.split())
27
+
28
+ return result, f"πŸ“Š Word Count: {word_count}"
29
+ except Exception as e:
30
+ return f"⚠️ Error: {str(e)}", ""
31
+
32
+ # Function to clear inputs and outputs
33
+ def clear_inputs():
34
+ return "", None, "", "", None
35
+
36
+ # Function to generate downloadable text file
37
+ def generate_downloadable_file(text):
38
+ if text.strip():
39
+ file_path = "analysis_result.txt"
40
+ with open(file_path, "w", encoding="utf-8") as f:
41
+ f.write(text)
42
+ return file_path
43
+ else:
44
+ return None
45
+
46
+ # Create Gradio UI
47
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
48
+ gr.Markdown("""
49
+ # πŸ“„ **AI-Powered Text & File Analyzer**
50
+ πŸš€ Upload a `.txt` file or enter text manually to get an AI-generated analysis and summary.
51
+ """)
52
+
53
+ with gr.Row():
54
+ text_input = gr.Textbox(label="✍️ Enter Text", placeholder="Type or paste your text here...", lines=6)
55
+ file_input = gr.File(label="πŸ“‚ Upload Text File (.txt)", type="filepath")
56
+
57
+ output_text = gr.Textbox(label="πŸ“ Analysis Result", lines=10, interactive=False)
58
+ word_count_display = gr.Textbox(label="πŸ“Š Word Count", interactive=False)
59
+
60
+ with gr.Row():
61
+ analyze_button = gr.Button("πŸ” Analyze", variant="primary")
62
+ clear_button = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
63
+
64
+ with gr.Column():
65
+ gr.Markdown("### πŸ“₯ Download Analysis Result")
66
+ with gr.Row():
67
+ download_button = gr.Button("⬇️ Download Result", variant="success", size="sm")
68
+ download_file = gr.File(label="πŸ“„ Click to Download", interactive=False)
69
+
70
+ # Button functionalities
71
+ analyze_button.click(analyze_input, inputs=[text_input, file_input], outputs=[output_text, word_count_display])
72
+ clear_button.click(clear_inputs, inputs=[], outputs=[text_input, file_input, output_text, word_count_display, download_file])
73
+ download_button.click(generate_downloadable_file, inputs=output_text, outputs=download_file)
74
+
75
+ # Launch the Gradio app
76
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ google-generativeai