Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,10 @@ from smolagents.models import TransformersModel
|
|
| 5 |
class SimpleAIAgent:
|
| 6 |
def __init__(self):
|
| 7 |
print("Initializing AI Agent...")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
self.model = TranformersModel("microsoft/DialoGPT-medium")
|
| 11 |
-
|
| 12 |
-
# Инструмент для поиска в интернете
|
| 13 |
self.search_tool = DuckDuckGoSearchTool()
|
| 14 |
-
|
| 15 |
-
# Создаем агента который может искать в интернете
|
| 16 |
self.agent = CodeAgent(
|
| 17 |
tools=[self.search_tool],
|
| 18 |
model=self.model,
|
|
@@ -26,8 +22,7 @@ class SimpleAIAgent:
|
|
| 26 |
Основная функция для общения с агентом
|
| 27 |
"""
|
| 28 |
print(f"User asked: {message}")
|
| 29 |
-
|
| 30 |
-
# Создаем инструкцию для агента
|
| 31 |
prompt = f"""
|
| 32 |
The user asked: {message}
|
| 33 |
|
|
@@ -37,10 +32,8 @@ class SimpleAIAgent:
|
|
| 37 |
"""
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
# Получаем ответ от агента
|
| 41 |
response = self.agent.run(prompt)
|
| 42 |
-
|
| 43 |
-
# Очищаем ответ от технических деталей
|
| 44 |
clean_response = self.clean_answer(response)
|
| 45 |
print(f"Agent replied: {clean_response[:100]}...")
|
| 46 |
|
|
@@ -59,33 +52,26 @@ class SimpleAIAgent:
|
|
| 59 |
clean_lines = []
|
| 60 |
|
| 61 |
for line in lines:
|
| 62 |
-
# Пропускаем строки про инструменты и процесс поиска
|
| 63 |
lower_line = line.lower()
|
| 64 |
if any(word in lower_line for word in ['tool:', 'searching', 'step', 'using tool']):
|
| 65 |
continue
|
| 66 |
|
| 67 |
-
# Пропускаем пустые строки в начале
|
| 68 |
if line.strip():
|
| 69 |
clean_lines.append(line)
|
| 70 |
-
|
| 71 |
-
# Собираем обратно в текст
|
| 72 |
result = '\n'.join(clean_lines).strip()
|
| 73 |
-
|
| 74 |
-
# Если ответ слишком длинный, обрезаем
|
| 75 |
if len(result) > 1500:
|
| 76 |
result = result[:1497] + "..."
|
| 77 |
|
| 78 |
return result if result else "I couldn't find a good answer to that question."
|
| 79 |
|
| 80 |
-
# Создаем экземпляр агента
|
| 81 |
ai_agent = SimpleAIAgent()
|
| 82 |
|
| 83 |
-
# Создаем интерфейс чата
|
| 84 |
with gr.Blocks(title="My AI Assistant") as chat_interface:
|
| 85 |
gr.Markdown("# My AI Assistant")
|
| 86 |
gr.Markdown("Ask me anything! I can search the internet for current information.")
|
| 87 |
-
|
| 88 |
-
# Создаем чат-интерфейс
|
| 89 |
chatbot = gr.Chatbot(height=400)
|
| 90 |
msg = gr.Textbox(
|
| 91 |
label="Your question",
|
|
@@ -95,17 +81,13 @@ with gr.Blocks(title="My AI Assistant") as chat_interface:
|
|
| 95 |
clear_btn = gr.Button("Clear Chat")
|
| 96 |
|
| 97 |
def respond(message, chat_history):
|
| 98 |
-
# Получаем ответ от агента
|
| 99 |
bot_response = ai_agent.chat(message, chat_history)
|
| 100 |
-
# Добавляем в историю чата
|
| 101 |
chat_history.append((message, bot_response))
|
| 102 |
return "", chat_history
|
| 103 |
|
| 104 |
-
# Обработчики событий
|
| 105 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 106 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 107 |
|
| 108 |
-
# Запускаем приложение
|
| 109 |
if __name__ == "__main__":
|
| 110 |
print("Starting AI Chat Assistant...")
|
| 111 |
chat_interface.launch(share=True)
|
|
|
|
| 5 |
class SimpleAIAgent:
|
| 6 |
def __init__(self):
|
| 7 |
print("Initializing AI Agent...")
|
| 8 |
+
self.model = TransformersModel("microsoft/DialoGPT-medium")
|
| 9 |
+
|
|
|
|
|
|
|
|
|
|
| 10 |
self.search_tool = DuckDuckGoSearchTool()
|
| 11 |
+
|
|
|
|
| 12 |
self.agent = CodeAgent(
|
| 13 |
tools=[self.search_tool],
|
| 14 |
model=self.model,
|
|
|
|
| 22 |
Основная функция для общения с агентом
|
| 23 |
"""
|
| 24 |
print(f"User asked: {message}")
|
| 25 |
+
|
|
|
|
| 26 |
prompt = f"""
|
| 27 |
The user asked: {message}
|
| 28 |
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 35 |
response = self.agent.run(prompt)
|
| 36 |
+
|
|
|
|
| 37 |
clean_response = self.clean_answer(response)
|
| 38 |
print(f"Agent replied: {clean_response[:100]}...")
|
| 39 |
|
|
|
|
| 52 |
clean_lines = []
|
| 53 |
|
| 54 |
for line in lines:
|
|
|
|
| 55 |
lower_line = line.lower()
|
| 56 |
if any(word in lower_line for word in ['tool:', 'searching', 'step', 'using tool']):
|
| 57 |
continue
|
| 58 |
|
|
|
|
| 59 |
if line.strip():
|
| 60 |
clean_lines.append(line)
|
| 61 |
+
|
|
|
|
| 62 |
result = '\n'.join(clean_lines).strip()
|
| 63 |
+
|
|
|
|
| 64 |
if len(result) > 1500:
|
| 65 |
result = result[:1497] + "..."
|
| 66 |
|
| 67 |
return result if result else "I couldn't find a good answer to that question."
|
| 68 |
|
|
|
|
| 69 |
ai_agent = SimpleAIAgent()
|
| 70 |
|
|
|
|
| 71 |
with gr.Blocks(title="My AI Assistant") as chat_interface:
|
| 72 |
gr.Markdown("# My AI Assistant")
|
| 73 |
gr.Markdown("Ask me anything! I can search the internet for current information.")
|
| 74 |
+
|
|
|
|
| 75 |
chatbot = gr.Chatbot(height=400)
|
| 76 |
msg = gr.Textbox(
|
| 77 |
label="Your question",
|
|
|
|
| 81 |
clear_btn = gr.Button("Clear Chat")
|
| 82 |
|
| 83 |
def respond(message, chat_history):
|
|
|
|
| 84 |
bot_response = ai_agent.chat(message, chat_history)
|
|
|
|
| 85 |
chat_history.append((message, bot_response))
|
| 86 |
return "", chat_history
|
| 87 |
|
|
|
|
| 88 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 89 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 90 |
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
print("Starting AI Chat Assistant...")
|
| 93 |
chat_interface.launch(share=True)
|