import time from crewai import Agent, Task, Crew from crewai.llm import LLM import gradio as gr # ---------------- LLM ---------------- llm = LLM( model="groq/llama-3.1-8b-instant", # ✅ WORKING GROQ MODEL temperature=0.2 ) # ---------------- AGENTS ---------------- researcher = Agent( role="Research Analyst", goal="Find key points", backstory="Expert researcher", llm=llm ) writer = Agent( role="Writer", goal="Write short blog", backstory="Creative writer", llm=llm ) editor = Agent( role="Editor", goal="Improve clarity and grammar", backstory="Professional editor", llm=llm ) # ---------------- FUNCTION ---------------- def generate_content(topic): try: if not topic.strip(): return "Please enter a topic." research_task = Task( description=f"Give ONLY 2 short points about '{topic}'.", agent=researcher, expected_output="2 bullet points" ) writing_task = Task( description=f"Write a short blog (50-60 words) on '{topic}'.", agent=writer, context=[research_task], expected_output="Short blog" ) editing_task = Task( description="Improve grammar and clarity.", agent=editor, context=[writing_task], expected_output="Final polished blog" ) crew = Crew( agents=[researcher, writer, editor], tasks=[research_task, writing_task, editing_task] ) # Retry logic for rate limit for i in range(3): try: result = crew.kickoff() time.sleep(2) return str(result) except Exception as e: print("Error:", e) if "RateLimitError" in str(e): time.sleep(10) else: return f"Error: {str(e)}" return "Too many requests. Please try again in a few seconds." except Exception as e: print("Final Error:", e) return f"Error: {str(e)}" # ---------------- UI ---------------- iface = gr.Interface( fn=generate_content, inputs=gr.Textbox(label="Enter Topic"), outputs=gr.Textbox(label="Generated Content"), title="Multi-Agent Content Creation Crew", description="AI agents collaborate to research, write, and edit content." ) # ---------------- LAUNCH ---------------- iface.launch(debug=True)