File size: 3,265 Bytes
4400447 | 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 | import gradio as gr
import pdfplumber
import openai
# Define the API Key directly without using environment variables
API_KEY = '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'
client = openai.OpenAI(api_key=API_KEY)
# Function to process PDF and interact with GPT-3, including temperature parameter
def process_pdf_and_query_gpt(pdf_file, temperature):
with pdfplumber.open(pdf_file.name) as pdf:
text = "\n".join([page.extract_text(x_tolerance=3, y_tolerance=3) or '' for page in pdf.pages])
tables = [page.extract_tables({"vertical_strategy": "text", "horizontal_strategy": "text"}) for page in pdf.pages]
instructions = """
You are a highly knowledgeable financial advisor with expertise in portfolio analysis and market trends. I have a few questions about the current performance and proposed changes to a client's portfolio:
1. **Portfolio Performance Assessment:** How is the portfolio currently performing? Would you consider its performance satisfactory? Please provide a detailed comparison of its performance against the benchmark over the last year and the last three years, including any significant metrics like the Sharpe ratio.
2. **Characteristics of Proposed Rebalancing:** Can you explain the characteristics of the rebalancing you are proposing? What specific changes are being suggested, and what are the key reasons behind these recommendations?
3. **Client's Risk Profile and Investment Constraints:** How does the proposed rebalancing align with the client’s risk profile and investment constraints? Discuss how the volatility levels and asset class allocations adhere to the client’s moderate risk objective.
4. **Market Movements and AI Model Insights:** Given the recent market movements and any shifts in volatility regimes, what insights has the AI model provided that are influencing this rebalancing decision? Highlight any significant shifts in asset class weightings and the rationale behind these adjustments.
5. **Expected Outcome of New Allocation:** Finally, what are the expected outcomes of this new allocation in the context of the current market phase? How does it position the portfolio in terms of risk and potential returns?
Your analysis will be invaluable in helping us understand the nuances of this portfolio management decision.
"""
input_data = f"""
---
My text :
{text}
---
My tables in text :
{str(tables)}
"""
completion = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[{"role": "system", "content": instructions}, {"role": "user", "content": input_data}],
temperature=temperature # Use the temperature parameter from the slider
)
response = completion.choices[0].message.content
return response
iface = gr.Interface(
fn=process_pdf_and_query_gpt,
inputs=[
gr.File(type="filepath"),
gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Temperature") # Corrected slider initialization
],
outputs="text",
title="Financial Analysis SPA"
)
# Run the app
iface.launch()
|