Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,35 +12,61 @@ load_dotenv()
|
|
12 |
# Access the Groq API key
|
13 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
"
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
|
|
30 |
def generate_article(topic):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
)
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Access the Groq API key
|
13 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
14 |
|
15 |
+
# Initialize the agent
|
16 |
+
try:
|
17 |
+
agent = Agent(
|
18 |
+
model=Groq(id="llama-3.3-70b-versatile", api_key=groq_api_key),
|
19 |
+
tools=[DuckDuckGo(), Newspaper4k()],
|
20 |
+
description="You are a senior NYT researcher writing an article on a topic.",
|
21 |
+
instructions=[
|
22 |
+
"For a given topic, search for the top 5 links.",
|
23 |
+
"Then read each URL and extract the article text, if a URL isn't available, ignore it.",
|
24 |
+
"Analyse and prepare an NYT-worthy article based on the information.",
|
25 |
+
],
|
26 |
+
markdown=True,
|
27 |
+
show_tool_calls=True,
|
28 |
+
add_datetime_to_instructions=True,
|
29 |
+
)
|
30 |
+
except Exception as e:
|
31 |
+
# Print error if the agent initialization fails
|
32 |
+
raise RuntimeError(f"Error initializing the agent: {e}")
|
33 |
|
34 |
+
# Function to process input and generate an article
|
35 |
def generate_article(topic):
|
36 |
+
if not topic.strip():
|
37 |
+
return "Please enter a valid topic."
|
38 |
+
|
39 |
+
try:
|
40 |
+
response = agent.run(topic) # Use `run` for robustness
|
41 |
+
# Handle different response types
|
42 |
+
if isinstance(response, str):
|
43 |
+
return response # Direct string output
|
44 |
+
elif isinstance(response, list):
|
45 |
+
return "\n".join(response) # List of strings
|
46 |
+
elif hasattr(response, 'content'): # Response object with `content` attribute
|
47 |
+
return response.content
|
48 |
+
else:
|
49 |
+
return f"Unexpected response type: {type(response)}. Raw output: {response}"
|
50 |
+
except Exception as e:
|
51 |
+
return f"Error generating the article: {e}"
|
52 |
+
|
53 |
+
# Gradio interface
|
54 |
+
with gr.Blocks() as app:
|
55 |
+
gr.Markdown("# 📰 NYT-Style Article Generator")
|
56 |
+
gr.Markdown(
|
57 |
+
"Enter a topic below, and the app will generate an NYT-style article by searching, extracting, and summarizing information from the web."
|
58 |
+
)
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
topic_input = gr.Textbox(
|
62 |
+
label="Enter Topic", placeholder="e.g., Simulation Theory", lines=1
|
63 |
+
)
|
64 |
+
generate_button = gr.Button("Generate Article")
|
65 |
+
|
66 |
+
output_text = gr.Markdown(label="Generated Article")
|
67 |
+
|
68 |
+
generate_button.click(fn=generate_article, inputs=topic_input, outputs=output_text)
|
69 |
+
|
70 |
+
# Run the app
|
71 |
+
if __name__ == "__main__":
|
72 |
+
app.launch()
|