arshad1234321 commited on
Commit
d62565a
·
verified ·
1 Parent(s): e94a9a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -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 = "AIzaSyAads6RKukWB3nIN2hvoTfYeWFhLu8oNsI"
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()