Tonic commited on
Commit
f2e5be8
1 Parent(s): 6b796ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -57
app.py CHANGED
@@ -72,66 +72,72 @@ def query_vectara(question):
72
  verify=True,
73
  headers=api_key_header
74
  )
 
 
 
 
 
 
 
 
75
 
76
- if response.status_code == 200:
77
- query_data = response.json()
78
- print(query_data)
79
- if query_data:
80
- sources_info = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- # Iterate over all response sets
83
- for response_set in query_data.get('responseSet', []):
84
- # Extract sources
85
- for source in response_set.get('response', []):
86
- source_metadata = source.get('metadata', [])
87
- source_info = {}
 
 
 
 
 
 
 
88
 
89
- for metadata in source_metadata:
90
- metadata_name = metadata.get('name', '')
91
- metadata_value = metadata.get('value', '')
92
 
93
- if metadata_name == 'title':
94
- source_info['title'] = metadata_value
95
- elif metadata_name == 'author':
96
- source_info['author'] = metadata_value
97
- elif metadata_name == 'pageNumber':
98
- source_info['page number'] = metadata_value
 
99
 
100
- if source_info:
101
- sources_info.append(source_info)
102
-
103
- return f"Sources:\n{json.dumps(sources_info, indent=2)}"
104
- else:
105
- return "No data found in the response."
106
  else:
107
- return f"Error: {response.status_code}"
108
-
109
-
110
- def convert_to_markdown(vectara_response):
111
- if vectara_response:
112
- response_set = vectara_response.get('responseSet', [{}])[0]
113
- summary = response_set.get('summary', [{}])[0]
114
- summary_text = summary.get('text', 'No summary available')
115
- sources = response_set.get('response', [])[:5]
116
- sources_text = [source.get('text', '') for source in sources]
117
-
118
- # Format the summary as Markdown
119
- markdown_summary = f'**Summary:** {summary_text}\n\n'
120
-
121
- # Format the sources as a numbered list
122
- markdown_sources = "\n".join([f"{i+1}. {source}" for i, source in enumerate(sources_text)])
123
-
124
- return f"{markdown_summary}**Sources:**\n{markdown_sources}"
125
- else:
126
- return "No data found in the response."
127
-
128
-
129
- iface = gr.Interface(
130
- fn=query_vectara,
131
- inputs=[gr.Textbox(label="Input Text")],
132
- outputs=[gr.Markdown(label="Output Text")],
133
- title="Vectara Chatbot",
134
- description="Ask me anything using the Vectara API!"
135
- )
136
-
137
- iface.launch()
 
72
  verify=True,
73
  headers=api_key_header
74
  )
75
+
76
+ if response.status_code == 200:
77
+ query_data = response.json()
78
+ if query_data:
79
+ sources_info = []
80
+
81
+ # Extract the summary.
82
+ summary = query_data['responseSet'][0]['summary'][0]['text']
83
 
84
+ # Iterate over all response sets
85
+ for response_set in query_data.get('responseSet', []):
86
+ # Extract sources
87
+ for source in response_set.get('response', [])[:5]: # Limit to top 5 sources.
88
+ source_metadata = source.get('metadata', [])
89
+ source_info = {}
90
+
91
+ for metadata in source_metadata:
92
+ metadata_name = metadata.get('name', '')
93
+ metadata_value = metadata.get('value', '')
94
+
95
+ if metadata_name == 'title':
96
+ source_info['title'] = metadata_value
97
+ elif metadata_name == 'author':
98
+ source_info['author'] = metadata_value
99
+ elif metadata_name == 'pageNumber':
100
+ source_info['page number'] = metadata_value
101
+
102
+ if source_info:
103
+ sources_info.append(source_info)
104
 
105
+ result = {"summary": summary, "sources": sources_info}
106
+ return f"{json.dumps(result, indent=2)}"
107
+ else:
108
+ return "No data found in the response."
109
+ else:
110
+ return f"Error: {response.status_code}"
111
+
112
+
113
+ def convert_to_markdown(vectara_response_json):
114
+ vectara_response = json.loads(vectara_response_json)
115
+ if vectara_response:
116
+ summary = vectara_response.get('summary', 'No summary available')
117
+ sources_info = vectara_response.get('sources', [])
118
 
119
+ # Format the summary as Markdown
120
+ markdown_summary = f'**Summary:** {summary}\n\n'
 
121
 
122
+ # Format the sources as a numbered list
123
+ markdown_sources = ""
124
+ for i, source_info in enumerate(sources_info):
125
+ author = source_info.get('author', 'Unknown author')
126
+ title = source_info.get('title', 'Unknown title')
127
+ page_number = source_info.get('page number', 'Unknown page number')
128
+ markdown_sources += f"{i+1}. {title} by {author}, Page {page_number}\n"
129
 
130
+ return f"{markdown_summary}**Sources:**\n{markdown_sources}"
 
 
 
 
 
131
  else:
132
+ return "No data found in the response."
133
+
134
+
135
+ iface = gr.Interface(
136
+ fn=lambda text: convert_to_markdown(query_vectara(text)),
137
+ inputs=[gr.Textbox(label="Input Text")],
138
+ outputs=[gr.Markdown(label="Output Text")],
139
+ title="Vectara Chatbot",
140
+ description="Ask me anything using the Vectara API!"
141
+ )
142
+
143
+ iface.launch()