Spaces:
Runtime error
Runtime error
Commit
Β·
6b08132
1
Parent(s):
d57b950
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,282 +1,531 @@
|
|
| 1 |
-
|
| 2 |
-
These are all the tools used in the NexusRaven V2 demo! You can provide any tools you want to Raven.
|
| 3 |
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
from typing import Dict, List
|
| 9 |
|
| 10 |
-
from
|
| 11 |
|
| 12 |
-
import
|
| 13 |
|
| 14 |
-
import
|
| 15 |
|
| 16 |
-
from
|
| 17 |
|
| 18 |
-
from
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
def __init__(self, config: DemoConfig) -> None:
|
| 23 |
-
self.config = config
|
| 24 |
|
| 25 |
-
|
| 26 |
-
self.client_ip: str | None = None
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
""
|
| 45 |
-
|
| 46 |
-
""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
""
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
- descending (bool): If descending is set, setting this boolean to true will sort the results such that the highest values are first.
|
| 69 |
-
- first_n (int): If provided, only retains the first n items in the final sorted list.
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
When people ask for 'best' or 'highest rated', sort by rating.
|
| 74 |
-
"""
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
key=lambda x: x.get(sort, float("inf")),
|
| 85 |
-
reverse=descending,
|
| 86 |
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
- location: This can be a city like 'Austin', or a place like 'Austin Airport', etc.
|
| 97 |
-
"""
|
| 98 |
-
if (
|
| 99 |
-
isinstance(location, list)
|
| 100 |
-
and len(location) != 0
|
| 101 |
-
and isinstance(location[0], dict)
|
| 102 |
-
):
|
| 103 |
-
return location
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
-
|
| 110 |
-
return []
|
| 111 |
-
print(results)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
latlong_1,
|
| 163 |
-
latlong_2,
|
| 164 |
-
f"The distance between {place_1} and {place_2} is {dist:.3f} miles",
|
| 165 |
-
]
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
- lat_long (tuple): The lat_long of interest.
|
| 173 |
-
"""
|
| 174 |
-
if len(lat_long) == 0:
|
| 175 |
-
return []
|
| 176 |
-
|
| 177 |
-
topic = " ".join(topics)
|
| 178 |
-
latlong = lat_long[0]["geometry"]["location"]
|
| 179 |
-
# For response format, see https://developers.google.com/maps/documentation/places/web-service/search-find-place#find-place-responses
|
| 180 |
-
results = self.gmaps.places(
|
| 181 |
-
query=topic,
|
| 182 |
-
location=latlong,
|
| 183 |
-
)
|
| 184 |
-
return results["results"]
|
| 185 |
|
| 186 |
-
|
| 187 |
-
self, type_of_place: list, location: str, radius_miles: int = 50
|
| 188 |
-
) -> List[Dict]:
|
| 189 |
-
"""
|
| 190 |
-
Find places close to a very defined location.
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
"""
|
| 196 |
-
place_details = self.get_latitude_longitude(location)
|
| 197 |
-
if len(place_details) == 0:
|
| 198 |
-
return []
|
| 199 |
-
place_details = place_details[0]
|
| 200 |
-
location = place_details["name"]
|
| 201 |
-
latlong = place_details["geometry"]["location"]
|
| 202 |
-
|
| 203 |
-
type_of_place = " ".join(type_of_place)
|
| 204 |
-
# Perform the search using Google Places API
|
| 205 |
-
# For response format, see https://developers.google.com/maps/documentation/places/web-service/search-nearby#nearby-search-responses
|
| 206 |
-
places_nearby = self.gmaps.places_nearby(
|
| 207 |
-
location=(latlong["lat"], latlong["lng"]),
|
| 208 |
-
keyword=type_of_place,
|
| 209 |
-
radius=radius_miles * 1609.34,
|
| 210 |
)
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
continue
|
| 226 |
|
| 227 |
-
|
| 228 |
-
place_nearby["distance"] = f"{distance} miles from {location}"
|
| 229 |
-
places.append(place_nearby)
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
"""
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
- place_names (list): The name of the establishment. This should be a physical location name. You can provide multiple inputs.
|
| 241 |
-
- location (str) : The location where the restaurant is located. Optional argument.
|
| 242 |
"""
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
-
|
| 275 |
-
review["for_location"] = place_name
|
| 276 |
-
review["formatted_address"] = place_details["formatted_address"]
|
| 277 |
|
| 278 |
-
all_reviews.extend(reviews)
|
| 279 |
|
| 280 |
-
|
| 281 |
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Callable, List, Tuple
|
|
|
|
| 2 |
|
| 3 |
+
import huggingface_hub
|
| 4 |
|
| 5 |
+
from dataclasses import dataclass
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from datetime import datetime
|
| 8 |
|
| 9 |
+
from time import sleep
|
| 10 |
|
| 11 |
+
import inspect
|
| 12 |
|
| 13 |
+
from random import randint
|
| 14 |
|
| 15 |
+
from urllib.parse import quote
|
| 16 |
|
| 17 |
+
from black import Mode, format_str
|
| 18 |
|
| 19 |
+
import gradio as gr
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
from huggingface_hub import InferenceClient
|
|
|
|
| 22 |
|
| 23 |
+
from constants import *
|
| 24 |
+
from config import DemoConfig
|
| 25 |
+
from tools import Tools
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@dataclass
|
| 29 |
+
class Function:
|
| 30 |
+
name: str
|
| 31 |
+
short_description: str
|
| 32 |
+
description_function: Callable[[Any], str]
|
| 33 |
+
explanation_function: Callable[[Any], str]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
FUNCTIONS = [
|
| 37 |
+
Function(
|
| 38 |
+
name="get_current_location",
|
| 39 |
+
short_description="Finding your city",
|
| 40 |
+
description_function=lambda *_, **__: "Finding your city",
|
| 41 |
+
explanation_function=lambda result: f"Found you in {result}!",
|
| 42 |
+
),
|
| 43 |
+
Function(
|
| 44 |
+
name="sort_results",
|
| 45 |
+
short_description="Sorting results",
|
| 46 |
+
description_function=lambda places, sort, descending=True, first_n=None: f"Sorting results by {sort} from "
|
| 47 |
+
+ ("lowest to highest" if not descending else "highest to lowest"),
|
| 48 |
+
explanation_function=lambda result: "Done!",
|
| 49 |
+
),
|
| 50 |
+
Function(
|
| 51 |
+
name="get_latitude_longitude",
|
| 52 |
+
short_description="Convert to coordinates",
|
| 53 |
+
description_function=lambda location: f"Converting {location} into latitude and longitude coordinates",
|
| 54 |
+
explanation_function=lambda result: "Converted!",
|
| 55 |
+
),
|
| 56 |
+
Function(
|
| 57 |
+
name="get_distance",
|
| 58 |
+
short_description="Calcuate distance",
|
| 59 |
+
description_function=lambda place_1, place_2: "Calculating distances",
|
| 60 |
+
explanation_function=lambda result: result[2],
|
| 61 |
+
),
|
| 62 |
+
Function(
|
| 63 |
+
name="get_recommendations",
|
| 64 |
+
short_description="Read recommendations",
|
| 65 |
+
description_function=lambda topics, **__: f"Reading recommendations for the following "
|
| 66 |
+
+ (
|
| 67 |
+
f"topics: {', '.join(topics)}" if len(topics) > 1 else f"topic: {topics[0]}"
|
| 68 |
+
),
|
| 69 |
+
explanation_function=lambda result: f"Read {len(result)} recommendations",
|
| 70 |
+
),
|
| 71 |
+
Function(
|
| 72 |
+
name="find_places_near_location",
|
| 73 |
+
short_description="Look for places",
|
| 74 |
+
description_function=lambda type_of_place, location, radius_miles=50: f"Looking for places near {location} within {radius_miles} with the following "
|
| 75 |
+
+ (
|
| 76 |
+
f"types: {', '.join(type_of_place)}"
|
| 77 |
+
if isinstance(type_of_place, list)
|
| 78 |
+
else f"type: {type_of_place}"
|
| 79 |
+
),
|
| 80 |
+
explanation_function=lambda result: f"Found "
|
| 81 |
+
+ (f"{len(result)} places!" if len(result) > 1 else f"1 place!"),
|
| 82 |
+
),
|
| 83 |
+
Function(
|
| 84 |
+
name="get_some_reviews",
|
| 85 |
+
short_description="Fetching reviews",
|
| 86 |
+
description_function=lambda place_names, **_: f"Fetching reviews for the requested items",
|
| 87 |
+
explanation_function=lambda result: f"Fetched {len(result)} reviews!",
|
| 88 |
+
),
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class FunctionsHelper:
|
| 93 |
+
FUNCTION_DEFINITION_TEMPLATE = '''Function:
|
| 94 |
+
def {name}{signature}:
|
| 95 |
+
"""
|
| 96 |
+
{docstring}
|
| 97 |
+
"""
|
| 98 |
|
| 99 |
+
'''
|
| 100 |
+
PROMPT_TEMPLATE = """{function_definitions}User Query: {query}<human_end>Call:"""
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
def __init__(self, tools: Tools) -> None:
|
| 103 |
+
self.tools = tools
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
function_definitions = ""
|
| 106 |
+
for function in FUNCTIONS:
|
| 107 |
+
f = getattr(tools, function.name)
|
| 108 |
+
signature = inspect.signature(f)
|
| 109 |
+
docstring = inspect.getdoc(f)
|
| 110 |
|
| 111 |
+
function_str = self.FUNCTION_DEFINITION_TEMPLATE.format(
|
| 112 |
+
name=function.name, signature=signature, docstring=docstring
|
| 113 |
+
)
|
| 114 |
+
function_definitions += function_str
|
| 115 |
|
| 116 |
+
self.prompt_without_query = self.PROMPT_TEMPLATE.format(
|
| 117 |
+
function_definitions=function_definitions, query="{query}"
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
+
def get_prompt(self, query: str):
|
| 121 |
+
return self.prompt_without_query.format(query=query)
|
| 122 |
+
|
| 123 |
+
def get_function_call_plan(self, function_call_str: str) -> List[str]:
|
| 124 |
+
function_call_list = []
|
| 125 |
+
locals_to_pass = {"function_call_list": function_call_list}
|
| 126 |
+
for f in FUNCTIONS:
|
| 127 |
+
name = f.name
|
| 128 |
+
exec(
|
| 129 |
+
f"def {name}(**_):\n\tfunction_call_list.append('{f.short_description}')",
|
| 130 |
+
locals_to_pass,
|
| 131 |
+
)
|
| 132 |
+
calls = [c.strip() for c in function_call_str.split(";") if c.strip()]
|
| 133 |
+
[eval(call, locals_to_pass) for call in calls]
|
| 134 |
+
return function_call_list
|
| 135 |
+
|
| 136 |
+
def run_function_call(self, function_call_str: str):
|
| 137 |
+
function_call_list = []
|
| 138 |
+
locals_to_pass = {"function_call_list": function_call_list, "tools": self.tools}
|
| 139 |
+
for f in FUNCTIONS:
|
| 140 |
+
name = f.name
|
| 141 |
+
|
| 142 |
+
locals_to_pass[f"{name}_description_function"] = f.description_function
|
| 143 |
+
locals_to_pass[f"{name}_explanation_function"] = f.explanation_function
|
| 144 |
+
|
| 145 |
+
function_definition = f"""
|
| 146 |
+
def {name}(**kwargs):
|
| 147 |
+
result = tools.{f.name}(**kwargs)
|
| 148 |
+
function_call_list.append(({name}_description_function(**kwargs), {name}_explanation_function(result)))
|
| 149 |
+
return result
|
| 150 |
+
"""
|
| 151 |
+
exec(function_definition, locals_to_pass)
|
| 152 |
|
| 153 |
+
calls = [c.strip() for c in function_call_str.split(";") if c.strip()]
|
| 154 |
+
for call in calls:
|
| 155 |
+
locals_to_pass["function_call_list"] = function_call_list = []
|
| 156 |
+
result = eval(call, locals_to_pass)
|
| 157 |
+
yield result, function_call_list
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
class RavenDemo(gr.Blocks):
|
| 161 |
+
def __init__(self, config: DemoConfig) -> None:
|
| 162 |
+
theme = gr.themes.Soft(
|
| 163 |
+
primary_hue=gr.themes.colors.blue,
|
| 164 |
+
secondary_hue=gr.themes.colors.blue,
|
| 165 |
)
|
| 166 |
+
super().__init__(theme=theme, css=CSS, title="NexusRaven V2 Demo")
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
self.config = config
|
| 169 |
+
self.tools = Tools(config)
|
| 170 |
+
self.functions_helper = FunctionsHelper(self.tools)
|
| 171 |
|
| 172 |
+
self.raven_client = InferenceClient(
|
| 173 |
+
model=config.raven_endpoint, token=config.hf_token
|
| 174 |
+
)
|
| 175 |
+
self.summary_model_client = InferenceClient(config.summary_model_endpoint)
|
| 176 |
+
|
| 177 |
+
self.max_num_steps = 20
|
| 178 |
+
|
| 179 |
+
with self:
|
| 180 |
+
gr.HTML(HEADER_HTML)
|
| 181 |
+
with gr.Row():
|
| 182 |
+
gr.Image(
|
| 183 |
+
"NexusRaven.png",
|
| 184 |
+
show_label=False,
|
| 185 |
+
show_share_button=True,
|
| 186 |
+
min_width=200,
|
| 187 |
+
scale=1,
|
| 188 |
+
)
|
| 189 |
+
with gr.Column(scale=4, min_width=800):
|
| 190 |
+
gr.Markdown(INTRO_TEXT, elem_classes="inner-large-font")
|
| 191 |
+
with gr.Row():
|
| 192 |
+
examples = [
|
| 193 |
+
gr.Button(query_name) for query_name in EXAMPLE_QUERIES
|
| 194 |
+
]
|
| 195 |
+
|
| 196 |
+
user_input = gr.Textbox(
|
| 197 |
+
placeholder="Ask me anything!",
|
| 198 |
+
show_label=False,
|
| 199 |
+
autofocus=True,
|
| 200 |
+
)
|
| 201 |
|
| 202 |
+
raven_function_call = gr.Code(
|
| 203 |
+
label="π¦ββ¬ NexusRaven V2 13B generated function call",
|
| 204 |
+
language="python",
|
| 205 |
+
interactive=False,
|
| 206 |
+
lines=10,
|
| 207 |
+
)
|
| 208 |
+
with gr.Accordion(
|
| 209 |
+
"Executing plan generated by π¦ββ¬ NexusRaven V2 13B", open=True
|
| 210 |
+
) as steps_accordion:
|
| 211 |
+
steps = [
|
| 212 |
+
gr.Textbox(visible=False, show_label=False)
|
| 213 |
+
for _ in range(self.max_num_steps)
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
with gr.Column():
|
| 217 |
+
initial_relevant_places = self.get_relevant_places([])
|
| 218 |
+
relevant_places = gr.State(initial_relevant_places)
|
| 219 |
+
place_dropdown_choices = self.get_place_dropdown_choices(
|
| 220 |
+
initial_relevant_places
|
| 221 |
+
)
|
| 222 |
+
places_dropdown = gr.Dropdown(
|
| 223 |
+
choices=place_dropdown_choices,
|
| 224 |
+
value=place_dropdown_choices[0],
|
| 225 |
+
label="Relevant places",
|
| 226 |
+
)
|
| 227 |
+
gmaps_html = gr.HTML(self.get_gmaps_html(initial_relevant_places[0]))
|
| 228 |
+
|
| 229 |
+
summary_model_summary = gr.Textbox(
|
| 230 |
+
label="Chat summary",
|
| 231 |
+
interactive=False,
|
| 232 |
+
show_copy_button=True,
|
| 233 |
+
lines=10,
|
| 234 |
+
max_lines=1000,
|
| 235 |
+
autoscroll=False,
|
| 236 |
+
elem_classes="inner-large-font",
|
| 237 |
+
)
|
| 238 |
|
| 239 |
+
with gr.Accordion("Raven inputs", open=False):
|
| 240 |
+
gr.Textbox(
|
| 241 |
+
label="Available functions",
|
| 242 |
+
value="`" + "`, `".join(f.name for f in FUNCTIONS) + "`",
|
| 243 |
+
interactive=False,
|
| 244 |
+
show_copy_button=True,
|
| 245 |
+
)
|
| 246 |
+
gr.Textbox(
|
| 247 |
+
label="Raven prompt",
|
| 248 |
+
value=self.functions_helper.get_prompt("{query}"),
|
| 249 |
+
interactive=False,
|
| 250 |
+
show_copy_button=True,
|
| 251 |
+
lines=20,
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
user_input.submit(
|
| 255 |
+
fn=self.on_submit,
|
| 256 |
+
inputs=[user_input],
|
| 257 |
+
outputs=[
|
| 258 |
+
user_input,
|
| 259 |
+
raven_function_call,
|
| 260 |
+
summary_model_summary,
|
| 261 |
+
relevant_places,
|
| 262 |
+
places_dropdown,
|
| 263 |
+
gmaps_html,
|
| 264 |
+
steps_accordion,
|
| 265 |
+
*steps,
|
| 266 |
+
],
|
| 267 |
+
concurrency_limit=20, # not a hyperparameter
|
| 268 |
+
api_name=False,
|
| 269 |
+
)
|
| 270 |
|
| 271 |
+
for i, button in enumerate(examples):
|
| 272 |
+
button.click(
|
| 273 |
+
fn=EXAMPLE_QUERIES.get,
|
| 274 |
+
inputs=button,
|
| 275 |
+
outputs=user_input,
|
| 276 |
+
api_name=f"button_click_{i}",
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
places_dropdown.input(
|
| 280 |
+
fn=self.get_gmaps_html_from_dropdown,
|
| 281 |
+
inputs=[places_dropdown, relevant_places],
|
| 282 |
+
outputs=gmaps_html,
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
def on_submit(self, query: str, request: gr.Request):
|
| 286 |
+
def get_returns():
|
| 287 |
+
return (
|
| 288 |
+
user_input,
|
| 289 |
+
raven_function_call,
|
| 290 |
+
summary_model_summary,
|
| 291 |
+
relevant_places,
|
| 292 |
+
places_dropdown,
|
| 293 |
+
gmaps_html,
|
| 294 |
+
steps_accordion,
|
| 295 |
+
*steps,
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
user_input = gr.Textbox(interactive=False)
|
| 299 |
+
raven_function_call = ""
|
| 300 |
+
summary_model_summary = ""
|
| 301 |
+
relevant_places = []
|
| 302 |
+
places_dropdown = ""
|
| 303 |
+
gmaps_html = ""
|
| 304 |
+
steps_accordion = gr.Accordion(open=True)
|
| 305 |
+
steps = [gr.Textbox(value="", visible=False) for _ in range(self.max_num_steps)]
|
| 306 |
+
yield get_returns()
|
| 307 |
+
|
| 308 |
+
raven_prompt = self.functions_helper.get_prompt(
|
| 309 |
+
query.replace("'", r"\'").replace('"', r"\"")
|
| 310 |
)
|
| 311 |
+
print(f"{'-' * 80}\nPrompt sent to Raven\n\n{raven_prompt}\n\n{'-' * 80}\n")
|
| 312 |
+
stream = self.raven_client.text_generation(
|
| 313 |
+
raven_prompt, **RAVEN_GENERATION_KWARGS
|
| 314 |
+
)
|
| 315 |
+
for s in stream:
|
| 316 |
+
for c in s:
|
| 317 |
+
raven_function_call += c
|
| 318 |
+
raven_function_call = raven_function_call.removesuffix("<bot_end>")
|
| 319 |
+
yield get_returns()
|
| 320 |
|
| 321 |
+
print(f"Raw Raven response before formatting: {raven_function_call}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
r_calls = [c.strip() for c in raven_function_call.split(";") if c.strip()]
|
| 324 |
+
f_r_calls = []
|
| 325 |
+
for r_c in r_calls:
|
| 326 |
+
f_r_call = format_str(r_c.strip(), mode=Mode())
|
| 327 |
+
f_r_calls.append(f_r_call)
|
| 328 |
|
| 329 |
+
raven_function_call = "; ".join(f_r_calls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
+
yield get_returns()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
+
self._set_client_ip(request)
|
| 334 |
+
function_call_plan = self.functions_helper.get_function_call_plan(
|
| 335 |
+
raven_function_call
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
)
|
| 337 |
+
for i, v in enumerate(function_call_plan):
|
| 338 |
+
steps[i] = gr.Textbox(value=f"{i+1}. {v}", visible=True)
|
| 339 |
+
yield get_returns()
|
| 340 |
+
sleep(0.1)
|
| 341 |
+
|
| 342 |
+
results_gen = self.functions_helper.run_function_call(raven_function_call)
|
| 343 |
+
results = []
|
| 344 |
+
previous_num_calls = 0
|
| 345 |
+
for result, function_call_list in results_gen:
|
| 346 |
+
results.extend(result)
|
| 347 |
+
for i, (description, explanation) in enumerate(function_call_list):
|
| 348 |
+
i = i + previous_num_calls
|
| 349 |
+
|
| 350 |
+
if len(description) > 100:
|
| 351 |
+
description = function_call_plan[i]
|
| 352 |
+
to_stream = f"{i+1}. {description} ..."
|
| 353 |
+
steps[i] = ""
|
| 354 |
+
for c in to_stream:
|
| 355 |
+
steps[i] += c
|
| 356 |
+
sleep(0.005)
|
| 357 |
+
yield get_returns()
|
| 358 |
+
|
| 359 |
+
to_stream = "." * randint(0, 5)
|
| 360 |
+
for c in to_stream:
|
| 361 |
+
steps[i] += c
|
| 362 |
+
sleep(0.2)
|
| 363 |
+
yield get_returns()
|
| 364 |
+
|
| 365 |
+
to_stream = f" {explanation}"
|
| 366 |
+
for c in to_stream:
|
| 367 |
+
steps[i] += c
|
| 368 |
+
sleep(0.005)
|
| 369 |
+
yield get_returns()
|
| 370 |
+
|
| 371 |
+
previous_num_calls += len(function_call_list)
|
| 372 |
+
|
| 373 |
+
relevant_places = self.get_relevant_places(results)
|
| 374 |
+
gmaps_html = self.get_gmaps_html(relevant_places[0])
|
| 375 |
+
places_dropdown_choices = self.get_place_dropdown_choices(relevant_places)
|
| 376 |
+
places_dropdown = gr.Dropdown(
|
| 377 |
+
choices=places_dropdown_choices, value=places_dropdown_choices[0]
|
| 378 |
+
)
|
| 379 |
+
steps_accordion = gr.Accordion(open=False)
|
| 380 |
+
yield get_returns()
|
| 381 |
+
|
| 382 |
+
while True:
|
| 383 |
+
try:
|
| 384 |
+
summary_model_prompt = self.get_summary_model_prompt(results, query)
|
| 385 |
+
print(
|
| 386 |
+
f"{'-' * 80}\nPrompt sent to summary model\n\n{summary_model_prompt}\n\n{'-' * 80}\n"
|
| 387 |
+
)
|
| 388 |
+
stream = self.summary_model_client.text_generation(
|
| 389 |
+
summary_model_prompt, **SUMMARY_MODEL_GENERATION_KWARGS
|
| 390 |
+
)
|
| 391 |
+
for s in stream:
|
| 392 |
+
for c in s:
|
| 393 |
+
summary_model_summary += c
|
| 394 |
+
summary_model_summary = (
|
| 395 |
+
summary_model_summary.lstrip().removesuffix(
|
| 396 |
+
"<|end_of_turn|>"
|
| 397 |
+
)
|
| 398 |
+
)
|
| 399 |
+
yield get_returns()
|
| 400 |
+
except huggingface_hub.inference._text_generation.ValidationError:
|
| 401 |
+
if len(results) > 1:
|
| 402 |
+
new_length = (3 * len(results)) // 4
|
| 403 |
+
results = results[:new_length]
|
| 404 |
+
continue
|
| 405 |
+
else:
|
| 406 |
+
break
|
| 407 |
+
|
| 408 |
+
break
|
| 409 |
+
|
| 410 |
+
user_input = gr.Textbox(interactive=True)
|
| 411 |
+
yield get_returns()
|
| 412 |
+
|
| 413 |
+
def get_summary_model_prompt(self, results: List, query: str) -> None:
|
| 414 |
+
# TODO check what outputs are returned and return them properly
|
| 415 |
+
ALLOWED_KEYS = [
|
| 416 |
+
"author_name",
|
| 417 |
+
"text",
|
| 418 |
+
"for_location",
|
| 419 |
+
"time",
|
| 420 |
+
"author_url",
|
| 421 |
+
"language",
|
| 422 |
+
"original_language",
|
| 423 |
+
"name",
|
| 424 |
+
"opening_hours",
|
| 425 |
+
"rating",
|
| 426 |
+
"user_ratings_total",
|
| 427 |
+
"vicinity",
|
| 428 |
+
"distance",
|
| 429 |
+
"formatted_address",
|
| 430 |
+
"price_level",
|
| 431 |
+
"types",
|
| 432 |
+
]
|
| 433 |
+
ALLOWED_KEYS = set(ALLOWED_KEYS)
|
| 434 |
+
|
| 435 |
+
results_str = ""
|
| 436 |
+
for idx, res in enumerate(results):
|
| 437 |
+
if isinstance(res, str):
|
| 438 |
+
results_str += f"{res}\n"
|
| 439 |
continue
|
| 440 |
|
| 441 |
+
assert isinstance(res, dict)
|
|
|
|
|
|
|
| 442 |
|
| 443 |
+
item_str = ""
|
| 444 |
+
for key, value in res.items():
|
| 445 |
+
if key not in ALLOWED_KEYS:
|
| 446 |
+
continue
|
| 447 |
|
| 448 |
+
key = key.replace("_", " ").capitalize()
|
| 449 |
+
item_str += f"\t{key}: {value}\n"
|
| 450 |
|
| 451 |
+
results_str += f"Result {idx + 1}\n{item_str}\n"
|
| 452 |
+
|
| 453 |
+
current_time = datetime.now().strftime("%b %d, %Y %H:%M:%S")
|
| 454 |
+
current_location = self.tools.get_current_location()
|
| 455 |
+
|
| 456 |
+
prompt = SUMMARY_MODEL_PROMPT.format(
|
| 457 |
+
current_location=current_location,
|
| 458 |
+
current_time=current_time,
|
| 459 |
+
results=results_str,
|
| 460 |
+
query=query,
|
| 461 |
+
)
|
| 462 |
+
return prompt
|
| 463 |
+
|
| 464 |
+
def get_relevant_places(self, results: List) -> List[Tuple[str, str]]:
|
| 465 |
"""
|
| 466 |
+
Returns
|
| 467 |
+
-------
|
| 468 |
+
relevant_places: List[Tuple[str, str]]
|
| 469 |
+
A list of tuples, where each tuple is (address, name)
|
| 470 |
|
|
|
|
|
|
|
| 471 |
"""
|
| 472 |
+
# We use a dict to preserve ordering, while enforcing uniqueness
|
| 473 |
+
relevant_places = dict()
|
| 474 |
+
for result in results:
|
| 475 |
+
if "formatted_address" in result and "name" in result:
|
| 476 |
+
relevant_places[(result["formatted_address"], result["name"])] = None
|
| 477 |
+
elif "formatted_address" in result and "for_location" in result:
|
| 478 |
+
relevant_places[
|
| 479 |
+
(result["formatted_address"], result["for_location"])
|
| 480 |
+
] = None
|
| 481 |
+
elif "vicinity" in result and "name" in result:
|
| 482 |
+
relevant_places[(result["vicinity"], result["name"])] = None
|
| 483 |
+
|
| 484 |
+
relevant_places = list(relevant_places.keys())
|
| 485 |
+
|
| 486 |
+
if not relevant_places:
|
| 487 |
+
current_location = self.tools.get_current_location()
|
| 488 |
+
relevant_places.append((current_location, current_location))
|
| 489 |
+
|
| 490 |
+
return relevant_places
|
| 491 |
+
|
| 492 |
+
def get_place_dropdown_choices(
|
| 493 |
+
self, relevant_places: List[Tuple[str, str]]
|
| 494 |
+
) -> List[str]:
|
| 495 |
+
return [p[1] for p in relevant_places]
|
| 496 |
+
|
| 497 |
+
def get_gmaps_html(self, relevant_place: Tuple[str, str]) -> str:
|
| 498 |
+
address, name = relevant_place
|
| 499 |
+
return GMAPS_EMBED_HTML_TEMPLATE.format(
|
| 500 |
+
address=quote(address), location=quote(name)
|
| 501 |
+
)
|
| 502 |
|
| 503 |
+
def get_gmaps_html_from_dropdown(
|
| 504 |
+
self, place_name: str, relevant_places: List[Tuple[str, str]]
|
| 505 |
+
) -> str:
|
| 506 |
+
relevant_place = [p for p in relevant_places if p[1] == place_name][0]
|
| 507 |
+
return self.get_gmaps_html(relevant_place)
|
| 508 |
+
|
| 509 |
+
def _set_client_ip(self, request: gr.Request) -> None:
|
| 510 |
+
client_ip = request.client.host
|
| 511 |
+
if (
|
| 512 |
+
"headers" in request.kwargs
|
| 513 |
+
and "x-forwarded-for" in request.kwargs["headers"]
|
| 514 |
+
):
|
| 515 |
+
x_forwarded_for = request.kwargs["headers"]["x-forwarded-for"]
|
| 516 |
+
else:
|
| 517 |
+
x_forwarded_for = request.headers.get("x-forwarded-for", None)
|
| 518 |
+
if x_forwarded_for:
|
| 519 |
+
client_ip = x_forwarded_for.split(",")[0].strip()
|
| 520 |
|
| 521 |
+
self.tools.client_ip = client_ip
|
|
|
|
|
|
|
| 522 |
|
|
|
|
| 523 |
|
| 524 |
+
demo = RavenDemo(DemoConfig.load_from_env())
|
| 525 |
|
| 526 |
+
if __name__ == "__main__":
|
| 527 |
+
demo.launch(
|
| 528 |
+
share=True,
|
| 529 |
+
allowed_paths=["logo.png", "NexusRaven.png"],
|
| 530 |
+
favicon_path="logo.png",
|
| 531 |
+
)
|