Spaces:
Sleeping
Sleeping
Fix chat history bleed bug and add professional README
Browse files
README.md
CHANGED
|
@@ -9,6 +9,73 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# 🚀 Crypto-Agent
|
| 13 |
|
| 14 |
Crypto-Agent, kullanıcıların sanal bir portföy ile kripto para alıp satmalarını sağlayan, tamamen yapay zeka (LLM) destekli bir **Tool Calling (Fonksiyon Çağırma)** asistanıdır.
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# 🚀 Crypto-Agent
|
| 13 |
+
### AI-Powered Autonomous Crypto Intelligence & Trading Simulator
|
| 14 |
+
|
| 15 |
+
Crypto-Agent, yerleşik yapay zeka entegrasyonu sayesinde kripto para piyasasını gerçek zamanlı analiz eden ve kullanıcıların sanal cüzdanlarıyla anlık fiyatlar üzerinden işlem (alım-satım) yapmasına olanak tanıyan otonom bir akıllı asistandır.
|
| 16 |
+
|
| 17 |
+
[](https://python.org)
|
| 18 |
+
[](https://gradio.app)
|
| 19 |
+
[](https://opensource.org/licenses/MIT)
|
| 20 |
+
|
| 21 |
+
**Hızlı Linkler:** [Features](#-features) · [How It Works](#-how-it-works) · [Installation](#-installation) · [Tech Stack](#-tech-stack)
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## ❓ What is Crypto-Agent?
|
| 26 |
+
Crypto-Agent, açık kaynaklı bir dil modelinin (Qwen 2.5 7B Instruct) dış API'ler ile doğrudan iletişim kurarak işlem yapabilmesini (Tool-Calling) sağlayan gelişmiş bir portföy yönetim sistemidir. Sistem halüsinasyon (olmayan veriyi uydurma) sorunlarını engellemek adına doğrudan Binance.US üzerinden saf veriler alır ve işlemleri anında yerel bir SQLite veritabanına yansıtır.
|
| 27 |
+
|
| 28 |
+
## ✨ Features
|
| 29 |
+
| Feature | Description |
|
| 30 |
+
|---------|-------------|
|
| 31 |
+
| 🤖 **Autonomous Tool Calling** | Yapay zeka kullanıcının isteğini analiz edip hangi fonksiyonu çağıracağına kendi karar verir (Fiyat çekme, Portföy görüntüleme, Trade). |
|
| 32 |
+
| 📊 **Real-time Price Fetching** | Binance.US API entegrasyonu sayesinde her zaman anlık ve gerçeğe uygun fiyatlar üzerinden işlem yapılır. |
|
| 33 |
+
| 💼 **Virtual Portfolio & DB** | SQLite tabanlı veritabanı ile kullanıcının sanal cüzdanı tutulur (Başlangıç 10.000$). |
|
| 34 |
+
| ⚡ **Math-Error Prevention** | LLM'lerin matematiksel hatalar yapmasını önlemek için "Dolarlık alım" işlemleri doğrudan arka uçta hesaplanır. |
|
| 35 |
+
| 🎨 **Clean & Minimal UI** | Kullanıcı deneyimi odaklı, kafa karıştırmayan sade bir Gradio Chat Arayüzü sunar. |
|
| 36 |
+
|
| 37 |
+
## 🧠 How It Works
|
| 38 |
+
Sistem, kullanıcının girdiği istemi (prompt) alır, yapılandırılmış sistem kurallarına göre analiz eder ve eyleme dönüştürür.
|
| 39 |
+
- **Input:** Kullanıcı işlemi belirtir (Örn: "1000 dolarlık BTC al").
|
| 40 |
+
- **Analysis:** LLM cümleyi yorumlar ve `execute_trade` fonksiyonunu kullanması gerektiğini anlar.
|
| 41 |
+
- **Data Retrieval:** Model, işlemi onaylamadan önce arka planda Binance'ten anlık BTC fiyatını çeker.
|
| 42 |
+
- **Execution:** Kripto miktarı arka planda hatasız hesaplanarak sanal veritabanına işlenir.
|
| 43 |
+
- **Synthesis:** Model işlemi tamamladığına dair formatlı ve kullanıcı dostu bir özet yanıt oluşturur.
|
| 44 |
+
|
| 45 |
+
## 🛠️ Installation
|
| 46 |
+
Projeyi yerel ortamınızda çalıştırmak için aşağıdaki adımları izleyebilirsiniz.
|
| 47 |
+
|
| 48 |
+
### 1. Clone the Repository
|
| 49 |
+
```bash
|
| 50 |
+
git clone https://github.com/MertAlii/Crypto-Agent.git
|
| 51 |
+
cd Crypto-Agent
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
### 2. Install Dependencies
|
| 55 |
+
> ⚠️ **Note:** Uygulamanın içerisinde Hugging Face altyapısına özel `spaces` dekoratörleri kullanılmaktadır. Yerelde çalıştırırken `spaces` modülünün kısıtlamalarına dikkat ediniz veya ilgili satırları devre dışı bırakınız.
|
| 56 |
+
|
| 57 |
+
```bash
|
| 58 |
+
pip install -r requirements.txt
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### 3. Start the Application
|
| 62 |
+
```bash
|
| 63 |
+
python app.py
|
| 64 |
+
```
|
| 65 |
+
Arayüz genellikle `http://127.0.0.1:7860/` adresinde yayınlanacaktır.
|
| 66 |
+
|
| 67 |
+
## 🏗️ Tech Stack
|
| 68 |
+
| Layer | Technology | Role |
|
| 69 |
+
|-------|------------|------|
|
| 70 |
+
| **Frontend UI** | Gradio (v5) | Sade ve minimal Chatbot arayüzü |
|
| 71 |
+
| **LLM Engine** | Qwen 2.5 7B Instruct | Dili anlama ve otonom karar verme |
|
| 72 |
+
| **Data Fetcher** | Python `requests` | Binance.US API'si ile anlık fiyat verisi |
|
| 73 |
+
| **Database** | SQLite3 | Kullanıcı varlıkları ve portföy geçmişi |
|
| 74 |
+
| **Environment** | Hugging Face Spaces | Sunucu, barındırma ve ZeroGPU |
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
*Built with ❤️ by Mert Ali.*
|
| 78 |
+
|
| 79 |
# 🚀 Crypto-Agent
|
| 80 |
|
| 81 |
Crypto-Agent, kullanıcıların sanal bir portföy ile kripto para alıp satmalarını sağlayan, tamamen yapay zeka (LLM) destekli bir **Tool Calling (Fonksiyon Çağırma)** asistanıdır.
|
app.py
CHANGED
|
@@ -52,27 +52,17 @@ def generate_response(messages):
|
|
| 52 |
generated_ids = outputs[0][len(inputs.input_ids[0]):]
|
| 53 |
return tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 54 |
|
| 55 |
-
def chat_interface(
|
| 56 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 57 |
-
|
| 58 |
-
for human, ai in history:
|
| 59 |
-
messages.append({"role": "user", "content": human})
|
| 60 |
-
if ai:
|
| 61 |
-
clean_content = ai.split("\n\n---\n\n🤖 **Asistan:** ")[-1] if "---\n\n🤖" in ai else ai.replace("🤖 **Asistan:** ", "")
|
| 62 |
-
messages.append({"role": "assistant", "content": clean_content})
|
| 63 |
-
|
| 64 |
-
messages.append({"role": "user", "content": user_message})
|
| 65 |
-
|
| 66 |
tool_logs = []
|
| 67 |
|
| 68 |
for _ in range(4): # Max 4 zincirleme tool çalıştırabilir
|
| 69 |
-
response_text = generate_response(
|
| 70 |
tool_calls = parse_tool_calls(response_text)
|
| 71 |
|
| 72 |
if not tool_calls:
|
| 73 |
break
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
for call in tool_calls:
|
| 78 |
name = call.get("name")
|
|
@@ -82,7 +72,7 @@ def chat_interface(user_message, history):
|
|
| 82 |
log = f"> **⚙️ Sistem İşlemi:** `{name}`\n> 📥 Parametreler: `{json.dumps(args, ensure_ascii=False)}`\n> 📤 Sonuç: `{tool_result}`"
|
| 83 |
tool_logs.append(log)
|
| 84 |
|
| 85 |
-
|
| 86 |
"role": "tool",
|
| 87 |
"name": name,
|
| 88 |
"content": str(tool_result)
|
|
@@ -93,7 +83,8 @@ def chat_interface(user_message, history):
|
|
| 93 |
final_output += "\n\n".join(tool_logs) + "\n\n---\n\n"
|
| 94 |
final_output += "🤖 **Asistan:**\n" + response_text
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
def get_balance_ui():
|
| 99 |
portfolio = get_portfolio()
|
|
@@ -115,6 +106,8 @@ with gr.Blocks(title="Kripto Asistan", css=css, theme=gr.themes.Soft()) as demo:
|
|
| 115 |
gr.Markdown("# 🚀 Kripto Portföy Yöneticisi")
|
| 116 |
gr.Markdown("Kripto para piyasasını analiz eden ve sanal bakiye ile alım-satım yapabilen akıllı asistan.")
|
| 117 |
|
|
|
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
balance_panel = gr.Markdown(get_balance_ui())
|
|
@@ -128,24 +121,28 @@ with gr.Blocks(title="Kripto Asistan", css=css, theme=gr.themes.Soft()) as demo:
|
|
| 128 |
msg = gr.Textbox(label="Komut", placeholder="Örn: 500 dolarlık SOL almak istiyorum...")
|
| 129 |
clear = gr.ClearButton([msg, chatbot])
|
| 130 |
|
| 131 |
-
def user_action(user_message: str, chat_history: list
|
| 132 |
chat_history.append([user_message, None])
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
def bot_action(chat_history: list
|
| 136 |
-
|
| 137 |
-
bot_response = chat_interface(user_message, chat_history[:-1])
|
| 138 |
chat_history[-1][1] = bot_response
|
| 139 |
-
return chat_history, get_balance_ui()
|
| 140 |
|
| 141 |
-
def update_balance()
|
| 142 |
return get_balance_ui()
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
refresh_btn.click(update_balance, None, balance_panel, api_name=False)
|
|
|
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
demo.launch()
|
|
|
|
| 52 |
generated_ids = outputs[0][len(inputs.input_ids[0]):]
|
| 53 |
return tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 54 |
|
| 55 |
+
def chat_interface(messages_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
tool_logs = []
|
| 57 |
|
| 58 |
for _ in range(4): # Max 4 zincirleme tool çalıştırabilir
|
| 59 |
+
response_text = generate_response(messages_state)
|
| 60 |
tool_calls = parse_tool_calls(response_text)
|
| 61 |
|
| 62 |
if not tool_calls:
|
| 63 |
break
|
| 64 |
|
| 65 |
+
messages_state.append({"role": "assistant", "content": response_text})
|
| 66 |
|
| 67 |
for call in tool_calls:
|
| 68 |
name = call.get("name")
|
|
|
|
| 72 |
log = f"> **⚙️ Sistem İşlemi:** `{name}`\n> 📥 Parametreler: `{json.dumps(args, ensure_ascii=False)}`\n> 📤 Sonuç: `{tool_result}`"
|
| 73 |
tool_logs.append(log)
|
| 74 |
|
| 75 |
+
messages_state.append({
|
| 76 |
"role": "tool",
|
| 77 |
"name": name,
|
| 78 |
"content": str(tool_result)
|
|
|
|
| 83 |
final_output += "\n\n".join(tool_logs) + "\n\n---\n\n"
|
| 84 |
final_output += "🤖 **Asistan:**\n" + response_text
|
| 85 |
|
| 86 |
+
messages_state.append({"role": "assistant", "content": response_text})
|
| 87 |
+
return final_output, messages_state
|
| 88 |
|
| 89 |
def get_balance_ui():
|
| 90 |
portfolio = get_portfolio()
|
|
|
|
| 106 |
gr.Markdown("# 🚀 Kripto Portföy Yöneticisi")
|
| 107 |
gr.Markdown("Kripto para piyasasını analiz eden ve sanal bakiye ile alım-satım yapabilen akıllı asistan.")
|
| 108 |
|
| 109 |
+
messages_state = gr.State([{"role": "system", "content": SYSTEM_PROMPT}])
|
| 110 |
+
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
balance_panel = gr.Markdown(get_balance_ui())
|
|
|
|
| 121 |
msg = gr.Textbox(label="Komut", placeholder="Örn: 500 dolarlık SOL almak istiyorum...")
|
| 122 |
clear = gr.ClearButton([msg, chatbot])
|
| 123 |
|
| 124 |
+
def user_action(user_message: str, chat_history: list, state: list):
|
| 125 |
chat_history.append([user_message, None])
|
| 126 |
+
state.append({"role": "user", "content": user_message})
|
| 127 |
+
return "", chat_history, state
|
| 128 |
|
| 129 |
+
def bot_action(chat_history: list, state: list):
|
| 130 |
+
bot_response, updated_state = chat_interface(state)
|
|
|
|
| 131 |
chat_history[-1][1] = bot_response
|
| 132 |
+
return chat_history, get_balance_ui(), updated_state
|
| 133 |
|
| 134 |
+
def update_balance():
|
| 135 |
return get_balance_ui()
|
| 136 |
|
| 137 |
+
def clear_state():
|
| 138 |
+
return [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 139 |
+
|
| 140 |
+
msg.submit(user_action, [msg, chatbot, messages_state], [msg, chatbot, messages_state], api_name=False).then(
|
| 141 |
+
bot_action, [chatbot, messages_state], [chatbot, balance_panel, messages_state], api_name=False
|
| 142 |
)
|
| 143 |
|
| 144 |
refresh_btn.click(update_balance, None, balance_panel, api_name=False)
|
| 145 |
+
clear.click(clear_state, None, messages_state, api_name=False)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
demo.launch()
|