Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,128 @@
|
|
| 1 |
-
import
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
from financials import financials
|
| 7 |
-
from workforce import workforce
|
| 8 |
-
|
| 9 |
-
# Set up OpenAI API
|
| 10 |
-
openai.api_key = "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"
|
| 11 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
Your responses should be brief, professional, and to the point, with a positive and energetic tone.
|
| 27 |
-
"""
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
response = openai.ChatCompletion.create(
|
| 31 |
model="llama-3.1-70b-versatile",
|
| 32 |
messages=[
|
| 33 |
-
{
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
]
|
| 36 |
)
|
| 37 |
return response.choices[0].message["content"]
|
| 38 |
except Exception as e:
|
| 39 |
-
return f"Error: {str(e)}"
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
+
from langdetect import detect
|
| 5 |
|
| 6 |
+
# Set up OpenAI API with your custom endpoint
|
| 7 |
+
openai.api_key = os.getenv("API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 9 |
|
| 10 |
+
# Import datasets from the Python files in your project
|
| 11 |
+
from datasets.company_profile import company_profile
|
| 12 |
+
from datasets.workforce import workforce
|
| 13 |
+
from datasets.financials import financials
|
| 14 |
+
from datasets.investors import investors
|
| 15 |
+
from datasets.products_services import products_services
|
| 16 |
+
from datasets.market_trends import market_trends
|
| 17 |
+
from datasets.partnerships_collaborations import partnerships_collaborations
|
| 18 |
+
from datasets.legal_compliance import legal_compliance
|
| 19 |
+
from datasets.customer_insights import customer_insights
|
| 20 |
+
from datasets.news_updates import news_updates
|
| 21 |
+
from datasets.social_media import social_media
|
| 22 |
+
from datasets.tech_stack import tech_stack
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Command handler for specific queries
|
| 25 |
+
def command_handler(user_input):
|
| 26 |
+
if user_input.lower().startswith("define "):
|
| 27 |
+
term = user_input[7:].strip()
|
| 28 |
+
definitions = {
|
| 29 |
+
"market analysis": (
|
| 30 |
+
"Market analysis is like peeking into the crystal ball of business! 🔮 It's where we gather "
|
| 31 |
+
"data about the market to forecast trends, track competition, and make smarter investment decisions!"
|
| 32 |
+
),
|
| 33 |
+
"financials": (
|
| 34 |
+
"Financial analysis is like the heartbeat of a company 💓. It tells us if the company is healthy, "
|
| 35 |
+
"sustainable, and ready to grow! 💰"
|
| 36 |
+
),
|
| 37 |
+
"investors": (
|
| 38 |
+
"Investors are like the superheroes of the business world 🦸♂️. They bring in the cash to fuel growth, "
|
| 39 |
+
"while hoping for big returns on their investment!"
|
| 40 |
+
)
|
| 41 |
+
}
|
| 42 |
+
return definitions.get(term.lower(), "Hmm, I don’t have a fun story for that term yet. Try another!")
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
# Function to get the response from OpenAI with humor and energy
|
| 46 |
+
def get_groq_response(message, user_language):
|
| 47 |
try:
|
| 48 |
response = openai.ChatCompletion.create(
|
| 49 |
model="llama-3.1-70b-versatile",
|
| 50 |
messages=[
|
| 51 |
+
{
|
| 52 |
+
"role": "system",
|
| 53 |
+
"content": (
|
| 54 |
+
f"You are a cheerful and energetic Private Market Analyst AI with a passion for explaining "
|
| 55 |
+
f"complex market analysis with humor, analogies, and wit. Keep it fun, engaging, and informative! "
|
| 56 |
+
f"Use your energy to keep the user excited and curious about market trends!"
|
| 57 |
+
)
|
| 58 |
+
},
|
| 59 |
+
{"role": "user", "content": message}
|
| 60 |
]
|
| 61 |
)
|
| 62 |
return response.choices[0].message["content"]
|
| 63 |
except Exception as e:
|
| 64 |
+
return f"Oops, looks like something went wrong! Error: {str(e)}"
|
| 65 |
+
|
| 66 |
+
# Function to handle the interaction and queries
|
| 67 |
+
def market_analysis_agent(user_input, history=[]):
|
| 68 |
+
try:
|
| 69 |
+
# Detect the language of the user's input
|
| 70 |
+
detected_language = detect(user_input)
|
| 71 |
+
user_language = "Hindi" if detected_language == "hi" else "English"
|
| 72 |
+
|
| 73 |
+
# Handle special commands like "Define [term]"
|
| 74 |
+
command_response = command_handler(user_input)
|
| 75 |
+
if command_response:
|
| 76 |
+
history.append((user_input, command_response))
|
| 77 |
+
return history, history
|
| 78 |
+
|
| 79 |
+
# Handle private market queries with datasets
|
| 80 |
+
if "company" in user_input.lower():
|
| 81 |
+
response = company_profile
|
| 82 |
+
elif "financials" in user_input.lower():
|
| 83 |
+
response = financials
|
| 84 |
+
elif "investors" in user_input.lower():
|
| 85 |
+
response = investors
|
| 86 |
+
elif "products" in user_input.lower():
|
| 87 |
+
response = products_services
|
| 88 |
+
elif "workforce" in user_input.lower():
|
| 89 |
+
response = workforce
|
| 90 |
+
else:
|
| 91 |
+
# Get dynamic AI response if query doesn't match predefined terms
|
| 92 |
+
response = get_groq_response(user_input, user_language)
|
| 93 |
+
|
| 94 |
+
# Add some cool and fun responses for engagement
|
| 95 |
+
cool_replies = [
|
| 96 |
+
"You're on fire! 🔥",
|
| 97 |
+
"Boom! 💥 That’s a market insight right there!",
|
| 98 |
+
"You’ve got this! 🚀",
|
| 99 |
+
"Let's keep that momentum going! 💎",
|
| 100 |
+
"That’s the power of market knowledge! 💪",
|
| 101 |
+
"You’re crushing it! 🎯"
|
| 102 |
+
]
|
| 103 |
+
response = f"{response} {cool_replies[hash(user_input) % len(cool_replies)]}"
|
| 104 |
+
|
| 105 |
+
# Add to chat history
|
| 106 |
+
history.append((user_input, response))
|
| 107 |
+
return history, history
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return [(user_input, f"Oops, something went wrong: {str(e)}")], history
|
| 111 |
+
|
| 112 |
+
# Gradio Interface setup
|
| 113 |
+
chat_interface = gr.Interface(
|
| 114 |
+
fn=market_analysis_agent, # Function for handling user interaction
|
| 115 |
+
inputs=["text", "state"], # Inputs: user message and chat history
|
| 116 |
+
outputs=["chatbot", "state"], # Outputs: chatbot messages and updated history
|
| 117 |
+
live=False, # Disable live responses; show after submit
|
| 118 |
+
title="Private Market AI Agent", # Title of the app
|
| 119 |
+
description=(
|
| 120 |
+
"Welcome to your cheerful and energetic Private Market Analyst! 🎉\n\n"
|
| 121 |
+
"Ask me anything about company profiles, market trends, financials, investors, and more! 🌟"
|
| 122 |
+
"I’ll break it down with jokes, stories, and humor to make market analysis a blast! 🚀"
|
| 123 |
+
)
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Launch the Gradio interface
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
chat_interface.launch()
|