Create app.py
Browse files
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,93 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import gradio as gr
         
     | 
| 2 | 
         
            +
            import os
         
     | 
| 3 | 
         
            +
            import shutil
         
     | 
| 4 | 
         
            +
             
     | 
| 5 | 
         
            +
            # Helper function: Generate a simple Python script
         
     | 
| 6 | 
         
            +
            def generate_code(description: str):
         
     | 
| 7 | 
         
            +
                templates = {
         
     | 
| 8 | 
         
            +
                    "hello world": "print('Hello, World!')",
         
     | 
| 9 | 
         
            +
                    "file reader": """
         
     | 
| 10 | 
         
            +
            with open('example.txt', 'r') as file:
         
     | 
| 11 | 
         
            +
                content = file.read()
         
     | 
| 12 | 
         
            +
            print(content)
         
     | 
| 13 | 
         
            +
            """,
         
     | 
| 14 | 
         
            +
                    "basic calculator": """
         
     | 
| 15 | 
         
            +
            def calculator(a, b, operation):
         
     | 
| 16 | 
         
            +
                if operation == 'add':
         
     | 
| 17 | 
         
            +
                    return a + b
         
     | 
| 18 | 
         
            +
                elif operation == 'subtract':
         
     | 
| 19 | 
         
            +
                    return a - b
         
     | 
| 20 | 
         
            +
                elif operation == 'multiply':
         
     | 
| 21 | 
         
            +
                    return a * b
         
     | 
| 22 | 
         
            +
                elif operation == 'divide' and b != 0:
         
     | 
| 23 | 
         
            +
                    return a / b
         
     | 
| 24 | 
         
            +
                else:
         
     | 
| 25 | 
         
            +
                    return 'Invalid operation or division by zero'
         
     | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
            +
            print(calculator(10, 5, 'add'))
         
     | 
| 28 | 
         
            +
            """
         
     | 
| 29 | 
         
            +
                }
         
     | 
| 30 | 
         
            +
                return templates.get(description.lower(), "Sorry, I don't have a template for that yet!")
         
     | 
| 31 | 
         
            +
             
     | 
| 32 | 
         
            +
            # Helper function: File conversion (e.g., .txt to .csv)
         
     | 
| 33 | 
         
            +
            def convert_file(file):
         
     | 
| 34 | 
         
            +
                base, ext = os.path.splitext(file.name)
         
     | 
| 35 | 
         
            +
                if ext == ".txt":
         
     | 
| 36 | 
         
            +
                    new_file = base + ".csv"
         
     | 
| 37 | 
         
            +
                    with open(file.name, "r") as f:
         
     | 
| 38 | 
         
            +
                        content = f.readlines()
         
     | 
| 39 | 
         
            +
                    with open(new_file, "w") as f:
         
     | 
| 40 | 
         
            +
                        for line in content:
         
     | 
| 41 | 
         
            +
                            f.write(",".join(line.split()) + "\n")
         
     | 
| 42 | 
         
            +
                    return new_file
         
     | 
| 43 | 
         
            +
                return "Unsupported file format for conversion."
         
     | 
| 44 | 
         
            +
             
     | 
| 45 | 
         
            +
            # Helper function: Basic diagnostics
         
     | 
| 46 | 
         
            +
            def diagnose_system(issue: str):
         
     | 
| 47 | 
         
            +
                suggestions = {
         
     | 
| 48 | 
         
            +
                    "slow pc": "Try clearing temporary files, disabling startup programs, and checking for malware.",
         
     | 
| 49 | 
         
            +
                    "internet issues": "Restart your router, check DNS settings, or contact your ISP.",
         
     | 
| 50 | 
         
            +
                    "high memory usage": "Check running processes in Task Manager and close unnecessary programs."
         
     | 
| 51 | 
         
            +
                }
         
     | 
| 52 | 
         
            +
                return suggestions.get(issue.lower(), "Sorry, I don't have a suggestion for that issue.")
         
     | 
| 53 | 
         
            +
             
     | 
| 54 | 
         
            +
            # Gradio Interface
         
     | 
| 55 | 
         
            +
            def main_interface(action, text_input=None, file_input=None):
         
     | 
| 56 | 
         
            +
                if action == "Generate Code":
         
     | 
| 57 | 
         
            +
                    return generate_code(text_input)
         
     | 
| 58 | 
         
            +
                elif action == "Convert File":
         
     | 
| 59 | 
         
            +
                    if file_input:
         
     | 
| 60 | 
         
            +
                        output_file = convert_file(file_input)
         
     | 
| 61 | 
         
            +
                        return f"File converted successfully: {output_file}"
         
     | 
| 62 | 
         
            +
                    return "Please upload a file for conversion."
         
     | 
| 63 | 
         
            +
                elif action == "Diagnose Issue":
         
     | 
| 64 | 
         
            +
                    return diagnose_system(text_input)
         
     | 
| 65 | 
         
            +
                else:
         
     | 
| 66 | 
         
            +
                    return "Invalid action selected."
         
     | 
| 67 | 
         
            +
             
     | 
| 68 | 
         
            +
            # Gradio app
         
     | 
| 69 | 
         
            +
            with gr.Blocks() as demo:
         
     | 
| 70 | 
         
            +
                gr.Markdown("# Tech Guru Bot - Your Personal Tech Assistant")
         
     | 
| 71 | 
         
            +
             
     | 
| 72 | 
         
            +
                with gr.Row():
         
     | 
| 73 | 
         
            +
                    action = gr.Radio(
         
     | 
| 74 | 
         
            +
                        ["Generate Code", "Convert File", "Diagnose Issue"],
         
     | 
| 75 | 
         
            +
                        label="Choose an action",
         
     | 
| 76 | 
         
            +
                        value="Generate Code",
         
     | 
| 77 | 
         
            +
                    )
         
     | 
| 78 | 
         
            +
                
         
     | 
| 79 | 
         
            +
                text_input = gr.Textbox(label="Enter your query or description")
         
     | 
| 80 | 
         
            +
                file_input = gr.File(label="Upload a file (for file conversion)", optional=True)
         
     | 
| 81 | 
         
            +
             
     | 
| 82 | 
         
            +
                output = gr.Textbox(label="Output", interactive=False)
         
     | 
| 83 | 
         
            +
             
     | 
| 84 | 
         
            +
                btn = gr.Button("Submit")
         
     | 
| 85 | 
         
            +
                btn.click(
         
     | 
| 86 | 
         
            +
                    fn=main_interface,
         
     | 
| 87 | 
         
            +
                    inputs=[action, text_input, file_input],
         
     | 
| 88 | 
         
            +
                    outputs=[output],
         
     | 
| 89 | 
         
            +
                )
         
     | 
| 90 | 
         
            +
             
     | 
| 91 | 
         
            +
            # Launch the Gradio app
         
     | 
| 92 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 93 | 
         
            +
                demo.launch()
         
     |