File size: 2,942 Bytes
ab96abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
import os

# Helper function: Generate a simple Python script
def generate_code(description: str):
    templates = {
        "hello world": "print('Hello, World!')",
        "file reader": """
with open('example.txt', 'r') as file:
    content = file.read()
print(content)
""",
        "basic calculator": """
def calculator(a, b, operation):
    if operation == 'add':
        return a + b
    elif operation == 'subtract':
        return a - b
    elif operation == 'multiply':
        return a * b
    elif operation == 'divide' and b != 0:
        return a / b
    else:
        return 'Invalid operation or division by zero'

print(calculator(10, 5, 'add'))
"""
    }
    return templates.get(description.lower(), "Sorry, I don't have a template for that yet!")

# Helper function: File conversion (e.g., .txt to .csv)
def convert_file(file):
    base, ext = os.path.splitext(file.name)
    if ext == ".txt":
        new_file = base + ".csv"
        with open(file.name, "r") as f:
            content = f.readlines()
        with open(new_file, "w") as f:
            for line in content:
                f.write(",".join(line.split()) + "\n")
        return f"File converted successfully: {new_file}"
    return "Unsupported file format for conversion."

# Helper function: Basic diagnostics
def diagnose_system(issue: str):
    suggestions = {
        "slow pc": "Try clearing temporary files, disabling startup programs, and checking for malware.",
        "internet issues": "Restart your router, check DNS settings, or contact your ISP.",
        "high memory usage": "Check running processes in Task Manager and close unnecessary programs."
    }
    return suggestions.get(issue.lower(), "Sorry, I don't have a suggestion for that issue.")

# Gradio Interface
def main_interface(action, text_input=None, file_input=None):
    if action == "Generate Code":
        return generate_code(text_input)
    elif action == "Convert File":
        if file_input:
            return convert_file(file_input)
        return "No file uploaded for conversion."
    elif action == "Diagnose Issue":
        return diagnose_system(text_input)
    else:
        return "Invalid action selected."

# Gradio app
with gr.Blocks() as demo:
    gr.Markdown("# Tech Guru Bot - Your Personal Tech Assistant")

    with gr.Row():
        action = gr.Radio(
            ["Generate Code", "Convert File", "Diagnose Issue"],
            label="Choose an action",
            value="Generate Code",
        )
    
    text_input = gr.Textbox(label="Enter your query or description")
    file_input = gr.File(label="Upload a file (for file conversion)")

    output = gr.Textbox(label="Output", interactive=False)

    btn = gr.Button("Submit")
    btn.click(
        fn=main_interface,
        inputs=[action, text_input, file_input],
        outputs=[output],
    )

# Launch the Gradio app
if __name__ == "__main__":
    demo.launch()