Spaces:
Sleeping
Sleeping
AliA1997 commited on
Commit ·
98f92bb
1
Parent(s): 5e6da92
Added custom_tools.py file
Browse files- .idea/.gitignore +8 -0
- .idea/BasicChatbot-AliA.iml +12 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +7 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- app.py +30 -23
- custom_tools.py +51 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/BasicChatbot-AliA.iml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="jdk" jdkName="Python 3.13" jdkType="Python SDK" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
<component name="PyDocumentationSettings">
|
| 9 |
+
<option name="format" value="PLAIN" />
|
| 10 |
+
<option name="myDocStringFormat" value="Plain" />
|
| 11 |
+
</component>
|
| 12 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="Black">
|
| 4 |
+
<option name="sdkName" value="Python 3.13" />
|
| 5 |
+
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" />
|
| 7 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/BasicChatbot-AliA.iml" filepath="$PROJECT_DIR$/.idea/BasicChatbot-AliA.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
app.py
CHANGED
|
@@ -1,5 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
|
@@ -11,33 +28,22 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
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
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
response =
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
|
@@ -46,6 +52,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
|
|
|
| 49 |
additional_inputs=[
|
| 50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, login
|
| 3 |
+
from custom_tools import suggest_best_cities, RecommendCountryTool
|
| 4 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, SpeechToTextTool, InferenceClientModel, FinalAnswerTool
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def create_agent(token: str):
|
| 9 |
+
login(token)
|
| 10 |
+
tools = [
|
| 11 |
+
DuckDuckGoSearchTool(),
|
| 12 |
+
WikipediaSearchTool(),
|
| 13 |
+
SpeechToTextTool(),
|
| 14 |
+
suggest_best_cities,
|
| 15 |
+
RecommendCountryTool(),
|
| 16 |
+
FinalAnswerTool()
|
| 17 |
+
]
|
| 18 |
+
model = InferenceClientModel()
|
| 19 |
+
return CodeAgent(tools=tools, model=model)
|
| 20 |
|
| 21 |
|
| 22 |
def respond(
|
|
|
|
| 28 |
top_p,
|
| 29 |
hf_token: gr.OAuthToken,
|
| 30 |
):
|
| 31 |
+
agent = create_agent(hf_token.token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if isinstance(message, tuple):
|
| 34 |
+
audio_path = message[0]
|
| 35 |
+
stt_tool = SpeechToTextTool()
|
| 36 |
+
message_text = stt_tool.transcribe(audio_path)
|
| 37 |
+
user_message = f"(Transcribed from audio) {message_text}"
|
| 38 |
+
else
|
| 39 |
+
user_message = message
|
| 40 |
|
| 41 |
+
full_prompt = f"{system_message}\n\nChat history:\n{history}\n\nUser: {user_message}"
|
| 42 |
|
| 43 |
+
response = agent.run(full_prompt, stream=False)
|
| 44 |
|
| 45 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
"""
|
|
|
|
| 52 |
chatbot = gr.ChatInterface(
|
| 53 |
respond,
|
| 54 |
type="messages",
|
| 55 |
+
multimodal=True,
|
| 56 |
additional_inputs=[
|
| 57 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 58 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
custom_tools.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool, Tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def suggest_best_cities(country: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Suggests a the best city based on the country
|
| 7 |
+
Args:
|
| 8 |
+
country (str): A city to get for a given country. Allowed values are:
|
| 9 |
+
- "jordan": Aqaba
|
| 10 |
+
- "syria": ALeppo
|
| 11 |
+
- "palestine": Gaza
|
| 12 |
+
- "usa": Sheriff Grady Judd
|
| 13 |
+
- "germany": Cologne
|
| 14 |
+
"""
|
| 15 |
+
if country == "jordan":
|
| 16 |
+
return "Aqaba"
|
| 17 |
+
elif country == "syria":
|
| 18 |
+
return "ALeppo"
|
| 19 |
+
elif country == "palestine":
|
| 20 |
+
return "Gaza"
|
| 21 |
+
elif country == "usa":
|
| 22 |
+
return "Sheriff Grady Judd"
|
| 23 |
+
elif country == "germany":
|
| 24 |
+
return "Cologne"
|
| 25 |
+
else:
|
| 26 |
+
return "Irbid"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class RecommendCountryTool(Tool):
|
| 30 |
+
name = "recommend_country_generator"
|
| 31 |
+
description = """
|
| 32 |
+
This tool suggests a best city based on the country/
|
| 33 |
+
It returns a unique city."""
|
| 34 |
+
|
| 35 |
+
inputs = {
|
| 36 |
+
"country": {
|
| 37 |
+
"type": "string",
|
| 38 |
+
"description": "The best city for the given country (e.g, 'jordan', 'syria', 'palestine')."
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
output_type = "string"
|
| 43 |
+
|
| 44 |
+
def forward(self, country: str):
|
| 45 |
+
countries = {
|
| 46 |
+
"jordan": "Aqaba",
|
| 47 |
+
"syria": "Aleppo",
|
| 48 |
+
"palestine": "Gaza City"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
return countries.get(country.lower(), "City for given country not found.")
|