SumanDhanu commited on
Commit
c67e9f8
·
verified ·
1 Parent(s): aa0b994

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -19
app.py CHANGED
@@ -93,32 +93,68 @@ editor_agent = Agent(
93
  model=model
94
  )
95
 
96
- # Helper function to safely run the agent
97
- async def run_agent_async(agent, prompt):
98
  try:
99
- result = await Runner.arun(agent, prompt)
100
- return result.final_output
 
 
 
 
 
101
  except Exception as e:
102
- return f"Error running agent: {str(e)}"
103
 
104
  # Workflow function for Gradio
105
  def fetch_and_edit_news(topic, language, search_date):
106
  try:
107
- # Create a new asyncio loop that works with nest_asyncio
108
- loop = asyncio.get_event_loop()
109
-
110
- # Step 1: Run the news agent
111
- news_prompt = f"Get me the news about {topic} in {language} for date {search_date}"
112
- raw_news = loop.run_until_complete(run_agent_async(news_agent, news_prompt))
113
-
114
- if raw_news.startswith("Error"):
115
- return raw_news
116
-
117
- # Step 2: Pass news to editor for final review
118
- editor_prompt = f"Please edit the following news in {language} language. Maintain the original language: \n\n{raw_news}"
119
- edited_news = loop.run_until_complete(run_agent_async(editor_agent, editor_prompt))
120
 
121
- return edited_news
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  except Exception as e:
123
  return f"Error: {str(e)}\n\nPlease check your OpenAI API key and ensure it has been set correctly in the repository secrets."
124
 
 
93
  model=model
94
  )
95
 
96
+ # Fallback to direct OpenAI API if agents don't work
97
+ def generate_with_openai(prompt):
98
  try:
99
+ response = client.chat.completions.create(
100
+ model="gpt-4o-mini",
101
+ messages=[{"role": "user", "content": prompt}],
102
+ temperature=0.7,
103
+ max_tokens=2000
104
+ )
105
+ return response.choices[0].message.content
106
  except Exception as e:
107
+ return f"Error with OpenAI API: {str(e)}"
108
 
109
  # Workflow function for Gradio
110
  def fetch_and_edit_news(topic, language, search_date):
111
  try:
112
+ # Initialize new event loop
113
+ loop = asyncio.new_event_loop()
114
+ asyncio.set_event_loop(loop)
 
 
 
 
 
 
 
 
 
 
115
 
116
+ # Try to use Runner.run_sync with the proper event loop context
117
+ try:
118
+ # Step 1: Run the news agent to get news
119
+ news_prompt = f"Get me the news about {topic} in {language} for date {search_date}"
120
+ news_result = Runner.run_sync(news_agent, news_prompt)
121
+ raw_news = news_result.final_output
122
+
123
+ # Step 2: Run the editor agent to edit the news
124
+ editor_prompt = f"Please edit the following news in {language} language. Maintain the original language: \n\n{raw_news}"
125
+ editor_result = Runner.run_sync(editor_agent, editor_prompt)
126
+ edited_news = editor_result.final_output
127
+
128
+ return edited_news
129
+
130
+ except Exception as agent_error:
131
+ print(f"Agent error: {agent_error}")
132
+
133
+ # Fallback to direct use of DuckDuckGo and OpenAI if agents fail
134
+ try:
135
+ print("Falling back to direct search and API calls...")
136
+ # Get news directly
137
+ raw_news = get_news_articles(topic, language, search_date)
138
+
139
+ if raw_news.startswith("Error") or raw_news.startswith("Could not find"):
140
+ return raw_news
141
+
142
+ # Format prompt for OpenAI
143
+ editor_prompt = f"""
144
+ Please edit and reformat the following news into a cohesive, publication-ready format.
145
+ Maintain the original language ({language}).
146
+ Each news story should be in a separate section with a clear headline:
147
+
148
+ {raw_news}
149
+ """
150
+
151
+ # Call OpenAI directly
152
+ edited_news = generate_with_openai(editor_prompt)
153
+ return edited_news
154
+
155
+ except Exception as fallback_error:
156
+ return f"Both agent-based and fallback approaches failed.\nError details: {str(fallback_error)}"
157
+
158
  except Exception as e:
159
  return f"Error: {str(e)}\n\nPlease check your OpenAI API key and ensure it has been set correctly in the repository secrets."
160