File size: 1,374 Bytes
4f038ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# franchise_continuity.py

import pandas as pd
import streamlit as st
import json
from utils import client

def analyze_franchise_continuity(thread_id, additional_context=None):
    if additional_context:
        client.beta.threads.messages.create(
            thread_id=thread_id,
            role="user",
            content=json.dumps(additional_context)
        )
    
    run = client.beta.threads.runs.create(
        thread_id=thread_id,
        assistant_id="asst_iirHFFSynUu1kBPRP8KT7wPl"
    )
    
    while run.status in ['queued', 'in_progress', 'cancelling']:
        run = client.beta.threads.runs.retrieve(
            thread_id=thread_id,
            run_id=run.id
        )
    
    if run.status == 'completed':
        messages = client.beta.threads.messages.list(thread_id=thread_id)
        analysis = next((msg.content[0].text.value for msg in reversed(list(messages)) if msg.role == "assistant"), "")
        return analysis
    else:
        return f"Error: Run status is {run.status}"

def process_franchise_continuity(analysis):
    try:
        df = pd.json_normalize(json.loads(analysis))
        st.dataframe(df)
    except Exception as e:
        st.error(f"Error processing data for Franchise Continuity: {e}")
        st.write("Please check the structure of the JSON data:")
        st.json(analysis)