patmakur commited on
Commit
d8ede0d
·
verified ·
1 Parent(s): a57a271

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -56,20 +56,34 @@ workflow.add_edge("researcher", "chart_generator")
56
  workflow.add_edge("chart_generator", END)
57
  graph = workflow.compile()
58
 
 
 
59
  def extract_chart_data(text):
60
  """
61
- Try to extract something like:
62
- 2018: 20
63
- 2019: 21.5
64
- 2020: 18
65
  """
66
- import re
67
- matches = re.findall(r'(\d{4})\s*[:\-]?\s*\$?([\d\.]+)', text)
68
  if not matches:
69
  return None, None
70
- years = [m[0] for m in matches]
71
- values = [float(m[1]) for m in matches]
72
- return years, values
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  def generate_plot(years, values):
75
  fig, ax = plt.subplots()
@@ -97,7 +111,9 @@ def run_langgraph(user_input):
97
 
98
  def process_input(user_input):
99
  result_text = run_langgraph(user_input)
 
100
  years, values = extract_chart_data(result_text)
 
101
 
102
  if years and values:
103
  chart = generate_plot(years, values)
@@ -107,6 +123,7 @@ def process_input(user_input):
107
 
108
 
109
 
 
110
  interface = gr.Interface(
111
  fn=process_input,
112
  inputs="text",
 
56
  workflow.add_edge("chart_generator", END)
57
  graph = workflow.compile()
58
 
59
+ import re
60
+
61
  def extract_chart_data(text):
62
  """
63
+ Extracts year-value pairs from text, supporting formats like:
64
+ - 2019: 21.5
65
+ - 2020 - $18.2
66
+ - GDP in 2021 was 20.5 trillion USD
67
  """
68
+ matches = re.findall(r'(\b19\d{2}|\b20\d{2})[^\d]{1,10}(\$?\d+(\.\d+)?)', text)
69
+
70
  if not matches:
71
  return None, None
72
+
73
+ years = []
74
+ values = []
75
+ for match in matches:
76
+ year = match[0]
77
+ value_str = match[1].replace('$', '')
78
+ try:
79
+ value = float(value_str)
80
+ years.append(year)
81
+ values.append(value)
82
+ except ValueError:
83
+ continue
84
+
85
+ return years, values if years and values else (None, None)
86
+
87
 
88
  def generate_plot(years, values):
89
  fig, ax = plt.subplots()
 
111
 
112
  def process_input(user_input):
113
  result_text = run_langgraph(user_input)
114
+ print("LLM Output:", result_text)
115
  years, values = extract_chart_data(result_text)
116
+ print("Extracted Data:", years, values)
117
 
118
  if years and values:
119
  chart = generate_plot(years, values)
 
123
 
124
 
125
 
126
+
127
  interface = gr.Interface(
128
  fn=process_input,
129
  inputs="text",