ibyteohdear commited on
Commit
8d29e19
·
verified ·
1 Parent(s): 45f1838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -187
app.py CHANGED
@@ -1,179 +1,11 @@
1
- import os
2
- import gradio as gr
3
- from smolagents import (
4
- tool,
5
- CodeAgent,
6
- DuckDuckGoSearchTool,
7
- InferenceClientModel,
8
- FinalAnswerTool,
9
- )
10
- from huggingface_hub import InferenceClient
11
- from fastapi import FastAPI
12
- import tempfile
13
- from PIL import Image
14
-
15
- def pil_to_tempfile(image):
16
-
17
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
18
- tmp_path = tmp.name
19
- tmp.close()
20
-
21
- image.save(tmp_path, format="PNG")
22
-
23
- return tmp_path
24
-
25
- token = os.getenv("HF_TOKEN")
26
-
27
- client = InferenceClient(token=token)
28
-
29
- nsfw_image_detection_client = InferenceClient(
30
- provider="hf-inference",
31
- api_key=token
32
- )
33
-
34
- text_to_image_client = InferenceClient(
35
- model="stabilityai/stable-diffusion-3-medium",
36
- api_key=token
37
- )
38
-
39
- @tool
40
- def nsfw_detection_tool(nsfw_detection_input: Image.Image) -> str:
41
- """
42
- Suitable for filtering through score explicit or inappropriate content in images.
43
- Args:
44
- nsfw_detection_input (Image.Image): The image to check.
45
- Returns:
46
- str: Highest score result.
47
- """
48
- try:
49
- outputs = client.image_classification(
50
- nsfw_detection_input,
51
- model="Falconsai/nsfw_image_detection"
52
- )
53
- top_result = max(outputs, key=lambda x: x.score)
54
- return f"{top_result.label.upper()}: {top_result.score:.1%}"
55
- except Exception as e:
56
- return f"Detection failed: {str(e)}"
57
-
58
- image_output = None
59
-
60
- @tool
61
- def image_tool(prompt: str) -> str:
62
- """
63
- Generate an image from text using SD3-Medium.
64
- Args:
65
- prompt (str): image description
66
- Returns:
67
- str: A confirmation message.
68
- """
69
- global image_output
70
-
71
- try:
72
- image = text_to_image_client.text_to_image(
73
- prompt=prompt,
74
- negative_prompt="blurry, distorted, low quality",
75
- guidance_scale=7.0,
76
- num_inference_steps=28,
77
- width=1024,
78
- height=1024
79
- )
80
- image_output = image
81
- return "Image successfully generated and stored for Gradio UI."
82
-
83
- except Exception as e:
84
- image_output = None
85
- print(f"Image generation failed: {e}")
86
- return f"Image generation failed: {e}"
87
-
88
- @tool
89
- def search_tool(query: str)-> str:
90
- """
91
- Search the web and return the most relevant results.
92
-
93
- Args:
94
- query (str): The search query.
95
-
96
- Returns:
97
- str: The search results.
98
- """
99
- web_search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
100
-
101
- results = web_search_tool(query)
102
-
103
- return results
104
-
105
- @tool
106
- def sentiment_tool(text: str) -> str:
107
- """
108
- Analyze sentiment of given text.
109
-
110
- Args:
111
- text (str): The sentiment query.
112
-
113
- Returns: str: sentiment
114
- """
115
- messages = [
116
- {"role": "system", "content": "Analyze the sentiment of the following text using a range score of 0 -> 10 and provied alternative wording"},
117
- {"role": "user", "content": text},
118
- ]
119
-
120
- completion = client.chat.completions.create(
121
- model="meta-llama/Llama-3.3-70B-Instruct",
122
- messages=messages,
123
- max_tokens=150,
124
- )
125
-
126
- result = completion.choices[0].message.content
127
-
128
- return result
129
-
130
- final_answer = FinalAnswerTool()
131
-
132
- model = InferenceClientModel(
133
- model_id="meta-llama/Llama-3.3-70B-Instruct",
134
- token=token,
135
- max_tokens=2096,
136
- temperature=0.5,
137
- )
138
-
139
- agent = CodeAgent(
140
- model=model,
141
- tools=[
142
- image_tool,
143
- nsfw_detection_tool,
144
- sentiment_tool,
145
- search_tool,
146
- final_answer,
147
- ],
148
- max_steps=6,
149
- planning_interval=None,
150
- )
151
-
152
- agent.prompt_templates["system_prompt"] += """
153
- You are a tool calling agent.
154
- You have access to these tools:
155
- - sentiment_tool(text: str) -> str
156
- - Analyze sentiment of given text.
157
- - search_tool(query: str) -> str
158
- - Search the web and return the most relevant results.
159
- - Used for sentiment analysis
160
- - image_tool(prompt: str) -> str
161
- - Generate an image from a text prompt, if successfull or not you will be notified by the return string.
162
- - nsfw_detection_tool(nsfw_detection_input: Image.Image) -> str
163
- - The nsfw_detection_input additional argument is processed entirely within the tool to produce a score from the input.
164
- - You must construct a well-formatted human-readable answer
165
- - You must introduce yourself as Jerry and greet the user in the answer
166
- - You must try include newlines, bullets, numbering, and proper punctuation
167
- - You must use this answer in final_answer
168
- """
169
-
170
- def run_agent(query: str, nsfw_detection_input: Image.Image):
171
  global image_output
172
  image_output = None
173
-
174
  try:
175
  response = agent.run(
176
- query,
177
  additional_args={"nsfw_detection_input": nsfw_detection_input}
178
  )
179
  return image_output, str(response)
@@ -183,25 +15,31 @@ def run_agent(query: str, nsfw_detection_input: Image.Image):
183
  with gr.Blocks(title="Jerry AI Assistant") as demo:
184
  gr.Markdown("# 🤖 Jerry - Your AI Assistant")
185
 
 
 
 
 
 
 
 
186
  with gr.Tab("Chat"):
187
  with gr.Row():
188
- query = gr.Textbox(
189
  lines=3,
190
  label="Ask me anything...",
191
- placeholder="Generate an image of a cat, analyze its sentiment, etc."
 
192
  )
193
 
194
  with gr.Row():
195
- run_btn = gr.Button("🚀 Run", variant="primary", scale=1)
196
- clear_btn = gr.Button("🗑️ Clear", scale=0)
197
 
198
- agent_response = gr.Textbox(
199
- label="Response",
200
- lines=5,
201
- interactive=False
202
  )
203
-
204
- run_btn.click(fn=run_agent, inputs=[query, None], outputs=[None, agent_response])
205
 
206
  with gr.Tab("Image Tools"):
207
  with gr.Row():
@@ -214,8 +52,29 @@ with gr.Blocks(title="Jerry AI Assistant") as demo:
214
  label="Generated Image",
215
  height=300
216
  )
217
-
218
- run_btn.click(fn=run_agent, inputs=[None, nsfw_detection_input], outputs=[image_output, agent_response])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  gr.Examples(
221
  examples=[
@@ -223,8 +82,6 @@ with gr.Blocks(title="Jerry AI Assistant") as demo:
223
  "What's the sentiment of: I love this product!",
224
  "Search for latest AI news"
225
  ],
226
- inputs=query,
227
  label="Quick Examples"
228
  )
229
-
230
- demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
 
1
+ def run_agent(query, nsfw_detection_input):
2
+ # Your existing run_agent function here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  global image_output
4
  image_output = None
5
+
6
  try:
7
  response = agent.run(
8
+ query if query else "",
9
  additional_args={"nsfw_detection_input": nsfw_detection_input}
10
  )
11
  return image_output, str(response)
 
15
  with gr.Blocks(title="Jerry AI Assistant") as demo:
16
  gr.Markdown("# 🤖 Jerry - Your AI Assistant")
17
 
18
+ # Shared output components
19
+ agent_response = gr.Textbox(
20
+ label="Response",
21
+ lines=5,
22
+ interactive=False
23
+ )
24
+
25
  with gr.Tab("Chat"):
26
  with gr.Row():
27
+ query_chat = gr.Textbox(
28
  lines=3,
29
  label="Ask me anything...",
30
+ placeholder="Generate an image of a cat, analyze its sentiment, etc.",
31
+ scale=4
32
  )
33
 
34
  with gr.Row():
35
+ run_chat_btn = gr.Button("🚀 Run", variant="primary", scale=1)
36
+ clear_chat_btn = gr.Button("🗑️ Clear", scale=0)
37
 
38
+ run_chat_btn.click(
39
+ fn=run_agent,
40
+ inputs=[query_chat, gr.Image(visible=False)], # Hidden image input for chat
41
+ outputs=[gr.Image(visible=False), agent_response]
42
  )
 
 
43
 
44
  with gr.Tab("Image Tools"):
45
  with gr.Row():
 
52
  label="Generated Image",
53
  height=300
54
  )
55
+
56
+ with gr.Row():
57
+ query_img = gr.Textbox(
58
+ lines=2,
59
+ label="Image generation prompt",
60
+ placeholder="A beautiful sunset over mountains..."
61
+ )
62
+
63
+ with gr.Row():
64
+ run_img_btn = gr.Button("🎨 Generate Image", variant="primary")
65
+ check_nsfw_btn = gr.Button("🔍 Check NSFW")
66
+
67
+ run_img_btn.click(
68
+ fn=run_agent,
69
+ inputs=[query_img, gr.Image(visible=False)],
70
+ outputs=[image_output, agent_response]
71
+ )
72
+
73
+ check_nsfw_btn.click(
74
+ fn=run_agent,
75
+ inputs=[gr.Textbox(visible=False), nsfw_detection_input],
76
+ outputs=[gr.Image(visible=False), agent_response]
77
+ )
78
 
79
  gr.Examples(
80
  examples=[
 
82
  "What's the sentiment of: I love this product!",
83
  "Search for latest AI news"
84
  ],
85
+ inputs=query_chat,
86
  label="Quick Examples"
87
  )