DrishtiSharma's picture
Update app.py
2b3379c verified
import gradio as gr
import os, threading
from multi_agent import run_multi_agent
lock = threading.Lock()
# Set environment variables for Hugging Face Spaces
os.environ["LANGCHAIN_PROJECT"] = os.getenv("LANGCHAIN_PROJECT", "langgraph-multi-agent")
LLM = "gpt-4o"
# Fetch the OpenAI API key from the environment
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise EnvironmentError("The environment variable 'OPENAI_API_KEY' is not set. Please configure it in your deployment.")
# Function to invoke the multi-agent process
def invoke(topic):
if not topic:
raise gr.Error("Topic is required.")
with lock:
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY # Set the API key from the environment
article = run_multi_agent(LLM, topic)
return article
gr.close_all()
# Fetch other environment variables with fallback defaults
default_topic = os.getenv("TOPIC", "Write an article about Artificial Intelligence.")
default_output = os.getenv("OUTPUT", "Your article will appear here after processing.")
default_description = os.getenv("DESCRIPTION", "Generate AI-written articles using Multi-Agent collaboration.")
# Gradio interface without API key input
demo = gr.Interface(
fn=invoke,
inputs=[
gr.Textbox(label="Topic", value=default_topic, lines=1)
],
outputs=[
gr.Markdown(label="Article", value=default_output)
],
title="Multi-Agent Article Writing",
description=default_description
)
demo.launch()