Manasa1 commited on
Commit
d47a636
·
verified ·
1 Parent(s): bb166c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -30
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
- agent = Agent(
16
- model=Groq(id="llama-3.3-70b-versatile", api_key=groq_api_key),
17
- tools=[DuckDuckGo(), Newspaper4k()],
18
- description="You are a senior NYT researcher writing an article on a topic.",
19
- instructions=[
20
- "For a given topic, search for the top 5 links.",
21
- "Then read each URL and extract the article text, if a URL isn't available, ignore it.",
22
- "Analyse and prepare an NYT worthy article based on the information.",
23
- ],
24
- markdown=True,
25
- show_tool_calls=True,
26
- add_datetime_to_instructions=True,
27
- # debug_mode=True,
28
- )
 
 
 
 
29
 
 
30
  def generate_article(topic):
31
- response = agent.print_response(topic, stream=True)
32
- # Collect the streamed responses
33
- full_response = ""
34
- for part in response:
35
- full_response += part
36
- return full_response
37
-
38
- iface = gr.Interface(
39
- fn=generate_article,
40
- inputs="text",
41
- outputs="text",
42
- title="NYT Article Generator",
43
- description="Enter a topic to generate an NYT-style article.",
44
- )
45
-
46
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()