Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from agno.agent import Agent
|
| 4 |
+
from agno.models.google.gemini import Gemini
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def llm(message, history: list[dict], api_key, num_conversation=3):
|
| 8 |
+
model = Gemini(api_key=api_key)
|
| 9 |
+
agent = Agent(model=model,
|
| 10 |
+
# tools=[WebsiteTools(), HackerNewsTools()],
|
| 11 |
+
description="Bạn là một trợ lý AI hữu ích và thông minh. Dưới đây là lịch sử hội thoại giữa bạn và người dùng:",
|
| 12 |
+
instructions=[f"Sử dụng kiến thức chuyên môn để trả lời câu hỏi hiện tại",
|
| 13 |
+
"Xem xét ngữ cảnh từ các câu hỏi và trả lời trước đó",
|
| 14 |
+
"Khi được hỏi về tin tức mới nhất mà không cung cấp thông tin cụ thể thì mặc định chọn 3 tin mới nhất từ HackerNews"],
|
| 15 |
+
show_tool_calls=True, markdown=True)
|
| 16 |
+
|
| 17 |
+
history.append({
|
| 18 |
+
"question": message["text"],
|
| 19 |
+
"answer": ""
|
| 20 |
+
})
|
| 21 |
+
conversation = ""
|
| 22 |
+
for entry in history[-num_conversation:]:
|
| 23 |
+
conversation += f"User: {entry['question']}\n"
|
| 24 |
+
conversation += f"Chatbot: {entry['answer']}\n\n"
|
| 25 |
+
|
| 26 |
+
result = agent.run(conversation).content
|
| 27 |
+
history[-1]["answer"] = result
|
| 28 |
+
return result
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
chat_history = []
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def process(message, history):
|
| 35 |
+
key = "AIzaSyBLLXxkA0Ij5iHr6tXgIcpwzCBANJQn4_o"
|
| 36 |
+
result = llm(message, chat_history, api_key=key, num_conversation=3)
|
| 37 |
+
temp = ""
|
| 38 |
+
for i in str(result).split():
|
| 39 |
+
time.sleep(0.05)
|
| 40 |
+
temp += i + " "
|
| 41 |
+
yield temp
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
gr.ChatInterface(
|
| 45 |
+
multimodal=True,
|
| 46 |
+
fn=process,
|
| 47 |
+
type="messages",
|
| 48 |
+
fill_width=True,
|
| 49 |
+
save_history=True
|
| 50 |
+
).launch()
|