File size: 5,534 Bytes
579038a
4c7ba35
579038a
 
4c7ba35
579038a
 
4c7ba35
 
579038a
 
 
 
 
 
 
 
 
 
 
 
 
eb2ec81
579038a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c7ba35
 
 
 
579038a
 
 
 
 
 
 
 
 
4c7ba35
 
 
 
579038a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
import os
import gradio as gr
import openai
from langdetect import detect

# Set up OpenAI API with your custom endpoint
openai.api_key = os.getenv("API_KEY")
openai.api_base = "https://api.groq.com/openai/v1"

# Import datasets from the Python files in your project
from datasets.company_profile import company_profile
from datasets.workforce import workforce
from datasets.financials import financials
from datasets.investors import investors
from datasets.products_services import products_services
from datasets.market_trends import market_trends
from datasets.partnerships_collaborations import partnerships_collaborations
from datasets.legal_compliance import legal_compliance
from datasets.customer_insights import customer_insights
from datasets.news_updates import news_updates
from datasets.social_media import social_media
from datasets.tech_stack import tech_stack

# Command handler for specific queries
def command_handler(user_input):
    if user_input.lower().startswith("define "):
        term = user_input[7:].strip()
        definitions = {
            "market analysis": (
                "Market analysis is like peeking into the crystal ball of business! 🔮 It's where we gather "
                "data about the market to forecast trends, track competition, and make smarter investment decisions!"
            ),
            "financials": (
                "Financial analysis is like the heartbeat of a company 💓. It tells us if the company is healthy, "
                "sustainable, and ready to grow! 💰"
            ),
            "investors": (
                "Investors are like the superheroes of the business world 🦸‍♂️. They bring in the cash to fuel growth, "
                "while hoping for big returns on their investment!"
            )
        }
        return definitions.get(term.lower(), "Hmm, I don’t have a fun story for that term yet. Try another!")
    return None

# Function to get the response from OpenAI with humor and energy
def get_groq_response(message, user_language):
    try:
        response = openai.ChatCompletion.create(
            model="llama-3.1-70b-versatile",
            messages=[
                {
                    "role": "system",
                    "content": (
                        f"You are a cheerful and energetic Private Market Analyst AI with a passion for explaining "
                        f"complex market analysis with humor, analogies, and wit. Keep it fun, engaging, and informative! "
                        f"Use your energy to keep the user excited and curious about market trends!"
                    )
                },
                {"role": "user", "content": message}
            ]
        )
        return response.choices[0].message["content"]
    except Exception as e:
        return f"Oops, looks like something went wrong! Error: {str(e)}"

# Function to handle the interaction and queries
def market_analysis_agent(user_input, history=[]):
    try:
        # Detect the language of the user's input
        detected_language = detect(user_input)
        user_language = "Hindi" if detected_language == "hi" else "English"
        
        # Handle special commands like "Define [term]"
        command_response = command_handler(user_input)
        if command_response:
            history.append((user_input, command_response))
            return history, history
        
        # Handle private market queries with datasets
        if "company" in user_input.lower():
            response = company_profile
        elif "financials" in user_input.lower():
            response = financials
        elif "investors" in user_input.lower():
            response = investors
        elif "products" in user_input.lower():
            response = products_services
        elif "workforce" in user_input.lower():
            response = workforce
        else:
            # Get dynamic AI response if query doesn't match predefined terms
            response = get_groq_response(user_input, user_language)

        # Add some cool and fun responses for engagement
        cool_replies = [
            "You're on fire! 🔥",
            "Boom! 💥 That’s a market insight right there!",
            "You’ve got this! 🚀",
            "Let's keep that momentum going! 💎",
            "That’s the power of market knowledge! 💪",
            "You’re crushing it! 🎯"
        ]
        response = f"{response} {cool_replies[hash(user_input) % len(cool_replies)]}"
        
        # Add to chat history
        history.append((user_input, response))
        return history, history

    except Exception as e:
        return [(user_input, f"Oops, something went wrong: {str(e)}")], history

# Gradio Interface setup
chat_interface = gr.Interface(
    fn=market_analysis_agent,  # Function for handling user interaction
    inputs=["text", "state"],  # Inputs: user message and chat history
    outputs=["chatbot", "state"],  # Outputs: chatbot messages and updated history
    live=False,  # Disable live responses; show after submit
    title="Private Market AI Agent",  # Title of the app
    description=(
        "Welcome to your cheerful and energetic Private Market Analyst! 🎉\n\n"
        "Ask me anything about company profiles, market trends, financials, investors, and more! 🌟"
        "I’ll break it down with jokes, stories, and humor to make market analysis a blast! 🚀"
    )
)

# Launch the Gradio interface
if __name__ == "__main__":
    chat_interface.launch()