Bloodlyghoul's picture
Create app.py
ab96abb verified
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()