| | import os |
| | import gradio as gr |
| | import google.generativeai as genai |
| | import chardet |
| |
|
| | |
| | GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") |
| |
|
| | if not GOOGLE_API_KEY: |
| | raise ValueError("β Error: Google API Key is missing. Set it in Hugging Face Secrets.") |
| |
|
| | |
| | genai.configure(api_key=GOOGLE_API_KEY) |
| |
|
| | |
| | model = genai.GenerativeModel(model_name="models/gemini-2.0-flash") |
| |
|
| | |
| | def read_file_with_encoding(file_path): |
| | try: |
| | with open(file_path, "rb") as f: |
| | raw_data = f.read() |
| |
|
| | |
| | encoding = chardet.detect(raw_data)["encoding"] |
| | if encoding is None: |
| | encoding = "utf-8" |
| |
|
| | |
| | with open(file_path, "r", encoding=encoding, errors="replace") as f: |
| | return f.read() |
| | except Exception as e: |
| | return f"β Error reading file: {str(e)}" |
| |
|
| | |
| | def analyze_input(text, file): |
| | try: |
| | if file is not None: |
| | text = read_file_with_encoding(file) |
| | elif not text.strip(): |
| | return "β Error: Please enter text or upload a file.", "" |
| |
|
| | text = text[:2000] |
| | prompt = f"Analyze and summarize this document:\n\n{text}" |
| | response = model.generate_content([prompt], stream=True) |
| |
|
| | |
| | result = "".join([chunk.text for chunk in response]) |
| | word_count = len(text.split()) |
| |
|
| | return result, f"π Word Count: {word_count}" |
| | except Exception as e: |
| | return f"β Error: {str(e)}", "" |
| |
|
| | |
| | def clear_inputs(): |
| | return "", None, "", "", None |
| |
|
| | |
| | def generate_downloadable_file(text): |
| | if text.strip(): |
| | file_path = "analysis_result.txt" |
| | with open(file_path, "w", encoding="utf-8") as f: |
| | f.write(text) |
| | return file_path |
| | else: |
| | return None |
| |
|
| | |
| | with gr.Blocks(theme=gr.themes.Default()) as demo: |
| | gr.Markdown(""" |
| | # π *AI-Powered Text & File Analyzer* |
| | π Upload a .txt file or enter text manually to get an AI-generated analysis and summary. |
| | """) |
| |
|
| | with gr.Row(): |
| | text_input = gr.Textbox(label="β Enter Text", placeholder="Type or paste your text here...", lines=6) |
| | file_input = gr.File(label="π Upload Text File (.txt)", type="filepath") |
| |
|
| | output_text = gr.Textbox(label="π Analysis Result", lines=10, interactive=False) |
| | word_count_display = gr.Textbox(label="π Word Count", interactive=False) |
| |
|
| | with gr.Row(): |
| | analyze_button = gr.Button("π Analyze", variant="primary") |
| | clear_button = gr.Button("π Clear", variant="secondary") |
| |
|
| | with gr.Column(): |
| | gr.Markdown("### π₯ Download Analysis Result") |
| | with gr.Row(): |
| | download_button = gr.Button("β¬ Download Result", variant="success", size="sm") |
| | download_file = gr.File(label="π Click to Download", interactive=False) |
| |
|
| | |
| | analyze_button.click(analyze_input, inputs=[text_input, file_input], outputs=[output_text, word_count_display]) |
| | clear_button.click(clear_inputs, inputs=[], outputs=[text_input, file_input, output_text, word_count_display, download_file]) |
| | download_button.click(generate_downloadable_file, inputs=output_text, outputs=download_file) |
| |
|
| | |
| | demo.launch() |
| |
|
| |
|