File size: 3,839 Bytes
9c12531
 
 
c7bc262
 
9c12531
c7bc262
9c12531
c7bc262
9c12531
c7bc262
9c12531
 
 
 
 
c7bc262
 
 
 
ce2af7e
c7bc262
 
 
ce2af7e
 
c7bc262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c12531
c7bc262
 
 
 
 
 
 
 
9c12531
c7bc262
 
 
 
 
 
 
 
 
 
 
 
 
 
0abdfaa
 
c7bc262
9c12531
 
 
 
 
 
 
 
c7bc262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import gradio as gr
from openai import OpenAI
import os
from crewai import Agent, Task, Crew, Process
from langchain_community.tools.tavily_search import TavilySearchResults

# Environment Variables
ACCESS_TOKEN = os.getenv("HF_TOKEN")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")

# OpenAI Client Initialization
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1/",
    api_key=ACCESS_TOKEN,
)

# Search Tool Initialization
search_tool = TavilySearchResults(tavily_api_key=TAVILY_API_KEY)

# System Prompt
SYSTEM_PROMPT = """
You are a highly knowledgeable and reliable Crypto Trading Advisor and Analyzer. 
Your goal is to assist users in understanding, analyzing, and making informed decisions about cryptocurrency trading. 
You provide accurate, concise, and actionable advice based on real-time data, historical trends, and established best practices.
"""

# CrewAI Integration
llm = client  # Using the OpenAI client for CrewAI agents

def run_crypto_crew(topic):
    researcher = Agent(
        role='Market Researcher',
        goal=f'Uncover emerging trends and investment opportunities in the cryptocurrency market. Focus on the topic: {topic}.',
        backstory='Identify groundbreaking trends and actionable insights.',
        verbose=True,
        tools=[search_tool],
        allow_delegation=False,
        llm=llm,
        max_iter=3,
        max_rpm=10,
    )

    analyst = Agent(
        role='Investment Analyst',
        goal=f'Analyze cryptocurrency market data to extract actionable insights. Focus on the topic: {topic}.',
        backstory='Draw meaningful conclusions from cryptocurrency market data.',
        verbose=True,
        allow_delegation=False,
        llm=llm,
    )

    research_task = Task(
        description=f'Explore the internet to identify trends and investment opportunities. Topic: {topic}.',
        agent=researcher,
        expected_output='Detailed summary of research results.'
    )

    analyst_task = Task(
        description=f'Analyze the market data to compile a concise report. Topic: {topic}.',
        agent=analyst,
        expected_output='Finalized version of the analysis report.'
    )

    crypto_crew = Crew(
        agents=[researcher, analyst],
        tasks=[research_task, analyst_task],
        process=Process.sequential
    )

    result = crypto_crew.kickoff()
    return result.raw

# Chatbot Response Function
def respond(message, history):
    max_tokens = 512
    temperature = 0.3
    top_p = 0.95
    frequency_penalty = 0.0
    seed = None

    if "analyze" in message.lower() or "trend" in message.lower():
        response = run_crypto_crew(message)
        yield response
    else:
        messages = [{"role": "system", "content": SYSTEM_PROMPT}]
        for user_part, assistant_part in history:
            if user_part:
                messages.append({"role": "user", "content": user_part})
            if assistant_part:
                messages.append({"role": "assistant", "content": assistant_part})
        messages.append({"role": "user", "content": message})

        response = ""
        for message_chunk in client.chat.completions.create(
            model="meta-llama/Llama-3.3-70B-Instruct",
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
            frequency_penalty=frequency_penalty,
            seed=seed,
            messages=messages,
        ):
            token_text = message_chunk.choices[0].delta.content
            response += token_text
            yield response

# Gradio UI
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Ask about crypto trading or analysis.")

demo = gr.ChatInterface(
    fn=respond,
    fill_height=True,
    chatbot=chatbot,
)

if __name__ == "__main__":
    demo.launch()