girishwangikar commited on
Commit
61d717a
1 Parent(s): 8646cd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -94,6 +94,12 @@ def generate_image(prompt):
94
  traceback.print_exc()
95
  return None
96
 
 
 
 
 
 
 
97
  def process_text(text, question):
98
  try:
99
  print("Creating graph...")
@@ -114,6 +120,10 @@ def process_text(text, question):
114
  graph_viz_path = visualize_graph(graph)
115
  print(f"Graph visualization saved to: {graph_viz_path}")
116
 
 
 
 
 
117
  print("Generating summary...")
118
  summary_prompt = f"Summarize the following text in one sentence: {text}"
119
  summary = llm.invoke(summary_prompt).content
@@ -126,29 +136,33 @@ def process_text(text, question):
126
  else:
127
  print("Failed to generate or save image")
128
 
129
- return answer, graph_viz_path, summary, image_path
130
  except Exception as e:
131
  print(f"An error occurred in process_text: {str(e)}")
132
  import traceback
133
  traceback.print_exc()
134
- return str(e), None, str(e), None
135
 
136
  def ui_function(text, question):
137
- answer, graph_viz_path, summary, image_path = process_text(text, question)
138
  if isinstance(answer, str) and answer.startswith("An error occurred"):
139
- return answer, None, answer, None
140
- return answer, graph_viz_path, summary, image_path
 
 
 
141
 
142
  # Create Gradio interface
143
  iface = gr.Interface(
144
  fn=ui_function,
145
  inputs=[
146
- gr.Textbox(label="Input Text"),
147
  gr.Textbox(label="Question")
148
  ],
149
  outputs=[
150
  gr.Textbox(label="Answer"),
151
  gr.Image(label="Graph Visualization", type="filepath"),
 
152
  gr.Textbox(label="Summary"),
153
  gr.Image(label="Generated Image", type="filepath")
154
  ],
 
94
  traceback.print_exc()
95
  return None
96
 
97
+ def create_relations_table(graph_documents_filtered):
98
+ df = pd.DataFrame(columns=['node1', 'node2', 'relation'])
99
+ for edge in graph_documents_filtered[0].relationships:
100
+ df = pd.concat([df, pd.DataFrame({'node1': [edge.source.id], 'node2': [edge.target.id], 'relation': [edge.type]})], ignore_index=True)
101
+ return df
102
+
103
  def process_text(text, question):
104
  try:
105
  print("Creating graph...")
 
120
  graph_viz_path = visualize_graph(graph)
121
  print(f"Graph visualization saved to: {graph_viz_path}")
122
 
123
+ print("Creating relations table...")
124
+ relations_table = create_relations_table(graph_documents_filtered)
125
+ print("Relations table created")
126
+
127
  print("Generating summary...")
128
  summary_prompt = f"Summarize the following text in one sentence: {text}"
129
  summary = llm.invoke(summary_prompt).content
 
136
  else:
137
  print("Failed to generate or save image")
138
 
139
+ return answer, graph_viz_path, relations_table, summary, image_path
140
  except Exception as e:
141
  print(f"An error occurred in process_text: {str(e)}")
142
  import traceback
143
  traceback.print_exc()
144
+ return str(e), None, None, str(e), None
145
 
146
  def ui_function(text, question):
147
+ answer, graph_viz_path, relations_table, summary, image_path = process_text(text, question)
148
  if isinstance(answer, str) and answer.startswith("An error occurred"):
149
+ return answer, None, None, answer, None
150
+ return answer, graph_viz_path, relations_table, summary, image_path
151
+
152
+ # Example text
153
+ example_text = """The Apollo 11 mission, launched by NASA in July 1969, was the first manned mission to land on the Moon. Commanded by Neil Armstrong and piloted by Buzz Aldrin and Michael Collins, it was the culmination of the Space Race between the United States and the Soviet Union. On July 20, 1969, Armstrong and Aldrin became the first humans to set foot on the lunar surface, while Collins orbited above in the command module."""
154
 
155
  # Create Gradio interface
156
  iface = gr.Interface(
157
  fn=ui_function,
158
  inputs=[
159
+ gr.Textbox(label="Input Text", value=example_text),
160
  gr.Textbox(label="Question")
161
  ],
162
  outputs=[
163
  gr.Textbox(label="Answer"),
164
  gr.Image(label="Graph Visualization", type="filepath"),
165
+ gr.Dataframe(label="Relations Table"),
166
  gr.Textbox(label="Summary"),
167
  gr.Image(label="Generated Image", type="filepath")
168
  ],