|
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 |
|
|
|
|
|
ACCESS_TOKEN = os.getenv("HF_TOKEN") |
|
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY") |
|
|
|
|
|
client = OpenAI( |
|
base_url="https://api-inference.huggingface.co/v1/", |
|
api_key=ACCESS_TOKEN, |
|
) |
|
|
|
|
|
search_tool = TavilySearchResults(tavily_api_key=TAVILY_API_KEY) |
|
|
|
|
|
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. |
|
""" |
|
|
|
|
|
llm = client |
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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() |
|
|