agentharbor commited on
Commit
0eb1396
·
verified ·
1 Parent(s): 2286e8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -60
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  from google import genai
4
  import json
5
 
@@ -34,12 +33,24 @@ def show_parts(r):
34
 
35
 
36
  client = genai.Client(api_key="AIzaSyD6voSAiSUim17kB90skpdisMMyFXZPxMo")
37
- MODEL_ID = "gemini-2.0-flash-exp"
38
 
39
  """
40
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
41
  """
42
  #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
43
  search_tool = {'google_search': {}}
44
  code_tool = {'code_execution':{}}
45
  tools = [
@@ -47,65 +58,65 @@ tools = [
47
  {'code_execution': {}}
48
  ]
49
  soccer_chat = client.chats.create(model="gemini-2.0-flash-exp", config={'tools': [search_tool]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- def respond(
52
- message,
53
- history: list[tuple[str, str]],
54
- system_message,
55
- max_tokens,
56
- temperature,
57
- top_p,
58
- ):
59
- messages = [{"role": "system", "content": system_message}]
60
-
61
- for val in history:
62
- if val[0]:
63
- messages.append({"role": "user", "content": val[0]})
64
- if val[1]:
65
- messages.append({"role": "assistant", "content": val[1]})
66
-
67
- messages.append({"role": "user", "content": message})
68
-
69
- response = ""
70
-
71
- '''
72
- for message in client.chat_completion(
73
- messages,
74
- max_tokens=max_tokens,
75
- stream=True,
76
- temperature=temperature,
77
- top_p=top_p,
78
- ):
79
- token = message.choices[0].delta.content
80
-
81
- response += token
82
- yield response
83
- '''
84
- r = soccer_chat.send_message(f'''{messages}''')
85
- response = show_parts(r)
86
- print (response)
87
- yield response
88
-
89
- """
90
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
91
- """
92
- demo = gr.ChatInterface(
93
- respond,
94
-
95
- additional_inputs=[
96
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
97
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
98
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
99
- gr.Slider(
100
- minimum=0.1,
101
- maximum=1.0,
102
- value=0.95,
103
- step=0.05,
104
- label="Top-p (nucleus sampling)"
105
- ),
106
- ],
107
  )
108
 
109
-
110
  if __name__ == "__main__":
111
- demo.launch()
 
1
  import gradio as gr
 
2
  from google import genai
3
  import json
4
 
 
33
 
34
 
35
  client = genai.Client(api_key="AIzaSyD6voSAiSUim17kB90skpdisMMyFXZPxMo")
36
+ MODEL_ID = "gemini-2.0-flash-thinking-exp"
37
 
38
  """
39
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
40
  """
41
  #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
42
+ import google.generativeai as genai2
43
+ genai2.configure(api_key=os.environ["GOOGLE_API_KEY"]
44
+ model_gen = genai2.GenerativeModel(model_name="gemini-2.0-flash-thinking-exp",
45
+ generation_config=generation_config,
46
+ system_instruction=system_instruction,
47
+ safety_settings=safety_settings)
48
+
49
+ )
50
+
51
+ def model_response(text):
52
+ response = model_gen.generate_content(text)
53
+ return response.text
54
  search_tool = {'google_search': {}}
55
  code_tool = {'code_execution':{}}
56
  tools = [
 
58
  {'code_execution': {}}
59
  ]
60
  soccer_chat = client.chats.create(model="gemini-2.0-flash-exp", config={'tools': [search_tool]})
61
+ coder_chat = client.chats.create(model="gemini-2.0-flash-exp", config={'tools': [code_tool]})
62
+
63
+ def agentville(problem):
64
+ memory = {}
65
+ output = ""
66
+ final_response = ""
67
+ plan = model_response(f'''You are a thinker. Think long and hard about the problem: {problem} and come up with the steps required to solve the problem. You are supposed to come up with 5 steps to build a solution.''')
68
+ print ("Plan", plan)
69
+ output += plan
70
+ yield output, final_response
71
+ for i in range(1,6):
72
+ print ("Step:", i)
73
+ output += f"Step {i} "
74
+ yield output, final_response
75
+ step = model_response(f'''Extract the {i}th step from the given plan: {plan}. Figure out which of the below agents are required to solve it.
76
+ Once you figure out the agent, just output the name of the agent.
77
+ Do not output anything else. The agents at your disposal are:
78
+ Researcher: Has access to Google search tool, can search for resources and answer the questions
79
+ Coder: Expert programmer. Can solve any problem at disposal. Output the name of the agent and nothing else.
80
+
81
+ Your response should be in the following format:
82
+ "Step": <the complete description of the step to be executed>
83
+ "agent_name": "Researcher/Coder"
84
+ ''')
85
+ print ("Current step", step)
86
+ output += step
87
+ yield output, final_response
88
+ if 'Coder' in step:
89
+ print ("Agent is coder")
90
+ r = coder_chat.send_message(f'''Complete the step:{step}''')
91
+ execution_step = show_parts(r)
92
+ output += execution_step
93
+ yield output, final_response
94
+ else:
95
+ print ("Agent is Researcher")
96
+ r = soccer_chat.send_message(f'''Complete the step: {step}''')
97
+ execution_step = show_parts(r)
98
+ output += execution_step
99
+ yield output, final_response
100
+ memory[i] = execution_step
101
+ final_response = model_response(f'''Given the problem statement:{problem} and the progress made by agents: {memory}, come up with the final answer. Do not explain what
102
+ the agents have done. Focus on getting the final answer.''')
103
+ print ("Final response", final_response)
104
+ output += final_response
105
+ yield output, final_response
106
+ return output, final_response
107
 
108
+ import gradio as gr
109
+ iface = gr.Interface(
110
+ fn=agentville,
111
+ #inputs=["text", "text", "text"],
112
+ inputs = gr.Textbox(label="Problem"),
113
+ outputs= [gr.Textbox(label="Agents processing"),
114
+ gr.Markdown(label="Final response")],
115
+ title="Agentville: Multi-agent autonomous system",
116
+ description="See multiple agents working in unison to solve complex problems",
117
+ theme = gr.themes.Ocean()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  )
119
 
120
+ # Launch the Gradio app
121
  if __name__ == "__main__":
122
+ iface.queue(max_size=20).launch(share=True,debug=True)