Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
google_api_key = os.getenv("MY_GOOGLE_AI_STUDIO_KEY")
|
| 6 |
+
|
| 7 |
+
# Write code to get gemini models
|
| 8 |
+
genai.configure(api_key = google_api_key)
|
| 9 |
+
model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 10 |
+
model_getFunctions = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
from UtilityFunctions import get_files_in_folder, get_file_types, organize_files, zip_folder, send_reminder_email, add_calendar_invite, send_stock_email, schedule_daily_stock_email
|
| 14 |
+
#Get function description
|
| 15 |
+
functions = 'function - get_files_in_folder - ' + get_files_in_folder.__doc__ + '''
|
| 16 |
+
function - get_file_types - ''' + get_file_types.__doc__ + '''
|
| 17 |
+
function - organize_files - ''' + organize_files.__doc__ + '''
|
| 18 |
+
function - zip_folder - ''' + zip_folder.__doc__ + '''
|
| 19 |
+
function - send_reminder_email - ''' + send_reminder_email.__doc__ + '''
|
| 20 |
+
function - add_calendar_invite - ''' + add_calendar_invite.__doc__ + '''
|
| 21 |
+
function - send_stock_email - ''' + send_stock_email.__doc__ + '''
|
| 22 |
+
function - schedule_daily_stock_email - ''' + schedule_daily_stock_email.__doc__
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def process_input(option):
|
| 26 |
+
if not option:
|
| 27 |
+
return "Please select an option", "from the dropbox"
|
| 28 |
+
|
| 29 |
+
prompt = f'''You are a AI assistant. Your job is to break a task into smaller
|
| 30 |
+
tasks that could be executed as code. Do not write the code, but just list the tasks as steps.
|
| 31 |
+
Here is your task - {option}'''
|
| 32 |
+
|
| 33 |
+
response = model.generate_content(prompt)
|
| 34 |
+
output1 = response.text
|
| 35 |
+
|
| 36 |
+
prompt2 = f'''You are a AI assistant. You are given some steps.
|
| 37 |
+
Your job is to get the function calls for these steps from the list of functions below.
|
| 38 |
+
Give me an executable python code that uses these functions. Just make the function calls.
|
| 39 |
+
Do not write any code within these functions. Do not strictly write any other code apart from the function calls.
|
| 40 |
+
However you are allowed to save the output of these functions into variables and use them as input parameters to other functions.
|
| 41 |
+
You shall only use the functions mentioned here and you shall not write your own code.
|
| 42 |
+
Your code should strictly not use any other functions apart from functions mentioned below.
|
| 43 |
+
Here are your steps - {response.text} ; and
|
| 44 |
+
here are your functions - {functions}'''
|
| 45 |
+
|
| 46 |
+
response2 = model_getFunctions.generate_content(prompt2)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
output2 = response2.text
|
| 50 |
+
return output1, output2
|
| 51 |
+
|
| 52 |
+
def handle_execute(code):
|
| 53 |
+
model_formatCode = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 54 |
+
if not code:
|
| 55 |
+
print("Empty String - nothing to be executed")
|
| 56 |
+
else:
|
| 57 |
+
# Split into lines, remove first and last, then join back
|
| 58 |
+
lines = code.splitlines()
|
| 59 |
+
cleaned_code = "\n".join(lines[1:-1])
|
| 60 |
+
print(cleaned_code)
|
| 61 |
+
exec(cleaned_code)
|
| 62 |
+
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# EPAI Capstone - Agents")
|
| 65 |
+
gr.Markdown('''This capstone demonstrate how agents work internally. This project accepts a user input,
|
| 66 |
+
breaks them into smaller tasks. Chooses from a predefined set of functions(tools) that correspond to these smaller tasks,
|
| 67 |
+
and, finally executes all the functions. \n\n Here are the set of available functions(tools):\n
|
| 68 |
+
def get_files_in_folder(root_dir) \n
|
| 69 |
+
def get_file_types(file_list) \n
|
| 70 |
+
def organize_files(type_dict) ''')
|
| 71 |
+
|
| 72 |
+
with gr.Column():
|
| 73 |
+
dropdown = gr.Dropdown(choices=['''scans a folder called MyDownlads, identify file types and
|
| 74 |
+
move them into categorized folders (e.g., PDFs, images, code files, etc)''',
|
| 75 |
+
'''scans a folder called MyDownlads, organizes files into categorized folders
|
| 76 |
+
(e.g., PDFs, images, code files, etc) and create a zip archive of each folder''',
|
| 77 |
+
'''Remind Ramya to submit TSAI assignment via email - sriramya.toleti@gmail.com''',
|
| 78 |
+
'''Send a calender invite to sriramya.toleti@gmail.com tomorrow at 7am to join my yoga class''',
|
| 79 |
+
'''Share the stock price for NVIDIA every day at 5 PM via email with sriramya.toleti@gmail.com''',
|
| 80 |
+
'''Write your own task
|
| 81 |
+
'''], label="Select an option", interactive=True)
|
| 82 |
+
text_input = gr.Textbox(label="Enter you task here", interactive=True, visible=False)
|
| 83 |
+
input_button = gr.Button("Submit")
|
| 84 |
+
output1 = gr.Textbox(label="Your task broken into smaller text")
|
| 85 |
+
output2 = gr.Textbox(label="Functions corresponding to your task from predefined toolbox")
|
| 86 |
+
execute_button = gr.Button("Execute", visible=False)
|
| 87 |
+
|
| 88 |
+
def toggle_textbox(option):
|
| 89 |
+
return gr.update(visible=(option.strip() == "Write your own task"))
|
| 90 |
+
|
| 91 |
+
dropdown.change(toggle_textbox, inputs=[dropdown], outputs=[text_input])
|
| 92 |
+
|
| 93 |
+
def handle_submit(option, text):
|
| 94 |
+
# If user selected "Write your own task", use the text input instead
|
| 95 |
+
task_input = text.strip() if option.strip() == "Write your own task" else option
|
| 96 |
+
if not task_input: # Ensure task_input is not empty
|
| 97 |
+
return "Please enter a task.", "", gr.update(visible=False)
|
| 98 |
+
|
| 99 |
+
out1, out2 = process_input(task_input) # Pass the correct task
|
| 100 |
+
return out1, out2, gr.update(visible=True)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
input_button.click(handle_submit, inputs=[dropdown, text_input],
|
| 104 |
+
outputs=[output1, output2, execute_button])
|
| 105 |
+
|
| 106 |
+
execute_button.click(handle_execute, inputs=[output2])
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
demo.launch()
|