SatyamSinghal's picture
Update app.py
579038a verified
raw
history blame
5.53 kB
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()