import requests from dotenv import load_dotenv import os import json import streamlit as st load_dotenv() BASE_API_URL = "https://api.langflow.astra.datastax.com" LANGFLOW_ID = "478dd003-8ac0-4d40-815e-77e7d1ae9343" FLOW_ID = "0853dfd7-558c-4958-9ca4-dc9ca8c69302" APPLICATION_TOKEN = os.environ.get("APP_TOKEN") ENDPOINT = "materialssciencebot" # Function to call API with improved error handling def run_flow(message: str): api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{ENDPOINT}" payload = { "input_value": message, "output_type": "chat", "input_type": "chat", } headers = {"Authorization": f"Bearer {APPLICATION_TOKEN}", "Content-Type": "application/json"} try: response = requests.post(api_url, json=payload, headers=headers, timeout=60, stream=True) response.raise_for_status() # Raise error for HTTP failures collected_response = "" for chunk in response.iter_content(chunk_size=1024): if chunk: collected_response += chunk.decode() yield collected_response # Stream output gradually except requests.exceptions.RequestException as e: yield f"Error: {str(e)}" except json.JSONDecodeError: yield "Error: Invalid JSON response from API" # Cache previous API responses to improve performance @st.cache_data def get_response(message: str): return list(run_flow(message))[-1] # Get final response st.markdown("""
""", unsafe_allow_html=True) def main(): st.title("Materials Science Bot") st.markdown("