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