ibyteohdear commited on
Commit
20a038a
·
verified ·
1 Parent(s): 8d29e19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -3
app.py CHANGED
@@ -1,5 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def run_agent(query, nsfw_detection_input):
2
- # Your existing run_agent function here
3
  global image_output
4
  image_output = None
5
 
@@ -15,7 +196,6 @@ def run_agent(query, nsfw_detection_input):
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,
@@ -37,7 +217,7 @@ with gr.Blocks(title="Jerry AI Assistant") as demo:
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
 
@@ -85,3 +265,6 @@ with gr.Blocks(title="Jerry AI Assistant") as demo:
85
  inputs=query_chat,
86
  label="Quick Examples"
87
  )
 
 
 
 
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
+
50
+ tmp_path = pil_to_tempfile(nsfw_detection_input)
51
+
52
+ outputs = client.image_classification(
53
+ tmp_path,
54
+ model="Falconsai/nsfw_image_detection"
55
+ )
56
+
57
+ os.remove(tmp_path)
58
+
59
+ top_result = max(outputs, key=lambda x: x.score)
60
+
61
+ verdict = (
62
+ f"Verdict: {top_result.label.upper()}\n"
63
+ f"Confidence: {top_result.score:.2%}"
64
+ )
65
+
66
+ return verdict
67
+
68
+ except Exception as e:
69
+ return f"NSFW detection failed: {e}"
70
+
71
+ image_output = None
72
+
73
+ @tool
74
+ def image_tool(prompt: str) -> str:
75
+ """
76
+ Generate an image from text using SD3-Medium.
77
+ Args:
78
+ prompt (str): image description
79
+ Returns:
80
+ str: A confirmation message.
81
+ """
82
+ global image_output
83
+
84
+ try:
85
+ image = text_to_image_client.text_to_image(
86
+ prompt=prompt,
87
+ negative_prompt="blurry, distorted, low quality",
88
+ guidance_scale=7.0,
89
+ num_inference_steps=28,
90
+ width=1024,
91
+ height=1024
92
+ )
93
+ image_output = image
94
+ return "Image successfully generated and stored for Gradio UI."
95
+
96
+ except Exception as e:
97
+ image_output = None
98
+ print(f"Image generation failed: {e}")
99
+ return f"Image generation failed: {e}"
100
+
101
+ @tool
102
+ def search_tool(query: str)-> str:
103
+ """
104
+ Search the web and return the most relevant results.
105
+
106
+ Args:
107
+ query (str): The search query.
108
+
109
+ Returns:
110
+ str: The search results.
111
+ """
112
+ web_search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
113
+
114
+ results = web_search_tool(query)
115
+
116
+ return results
117
+
118
+ @tool
119
+ def sentiment_tool(text: str) -> str:
120
+ """
121
+ Analyze sentiment of given text.
122
+
123
+ Args:
124
+ text (str): The sentiment query.
125
+
126
+ Returns: str: sentiment
127
+ """
128
+ messages = [
129
+ {"role": "system", "content": "Analyze the sentiment of the following text using a range score of 0 -> 10 and provied alternative wording"},
130
+ {"role": "user", "content": text},
131
+ ]
132
+
133
+ completion = client.chat.completions.create(
134
+ model="meta-llama/Llama-3.3-70B-Instruct",
135
+ messages=messages,
136
+ max_tokens=150,
137
+ )
138
+
139
+ result = completion.choices[0].message.content
140
+
141
+ return result
142
+
143
+ final_answer = FinalAnswerTool()
144
+
145
+ model = InferenceClientModel(
146
+ model_id="meta-llama/Llama-3.3-70B-Instruct",
147
+ token=token,
148
+ max_tokens=2096,
149
+ temperature=0.5,
150
+ )
151
+
152
+ agent = CodeAgent(
153
+ model=model,
154
+ tools=[
155
+ image_tool,
156
+ nsfw_detection_tool,
157
+ sentiment_tool,
158
+ search_tool,
159
+ final_answer,
160
+ ],
161
+ max_steps=6,
162
+ planning_interval=None,
163
+ )
164
+
165
+ agent.prompt_templates["system_prompt"] += """
166
+ You are a tool calling agent.
167
+ You have access to these tools:
168
+ - sentiment_tool(text: str) -> str
169
+ - Analyze sentiment of given text.
170
+ - search_tool(query: str) -> str
171
+ - Search the web and return the most relevant results.
172
+ - Used for sentiment analysis
173
+ - image_tool(prompt: str) -> str
174
+ - Generate an image from a text prompt, if successfull or not you will be notified by the return string.
175
+ - nsfw_detection_tool(nsfw_detection_input: Image.Image) -> str
176
+ - The nsfw_detection_input additional argument is processed entirely within the tool to produce a score from the input.
177
+ - You must construct a well-formatted human-readable answer
178
+ - You must introduce yourself as Jerry and greet the user in the answer
179
+ - You must try include newlines, bullets, numbering, and proper punctuation
180
+ - You must use this answer in final_answer
181
+ """
182
+
183
  def run_agent(query, nsfw_detection_input):
 
184
  global image_output
185
  image_output = None
186
 
 
196
  with gr.Blocks(title="Jerry AI Assistant") as demo:
197
  gr.Markdown("# 🤖 Jerry - Your AI Assistant")
198
 
 
199
  agent_response = gr.Textbox(
200
  label="Response",
201
  lines=5,
 
217
 
218
  run_chat_btn.click(
219
  fn=run_agent,
220
+ inputs=[query_chat, gr.Image(visible=False)],
221
  outputs=[gr.Image(visible=False), agent_response]
222
  )
223
 
 
265
  inputs=query_chat,
266
  label="Quick Examples"
267
  )
268
+
269
+ if __name__ == "__main__":
270
+ demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())