File size: 7,975 Bytes
6b9d2e8 e5f444f 7b96044 e5f444f 7b96044 e5f444f 6b9d2e8 7b96044 e5f444f 6b9d2e8 7b96044 6b9d2e8 e5f444f 7b96044 e5f444f 6b9d2e8 7b96044 6b9d2e8 7b96044 6b9d2e8 e5f444f 6b9d2e8 e5f444f 6b9d2e8 e5f444f 6b9d2e8 e5f444f 6b9d2e8 cb47347 6b9d2e8 e5f444f cb47347 e5f444f 6b9d2e8 7b96044 6b9d2e8 e5f444f 7b96044 e5f444f 7b96044 6b9d2e8 e5f444f 7b96044 e5f444f 7b96044 6b9d2e8 e5f444f 7b96044 6b9d2e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import gradio as gr
import requests
import skills
from skills.common import config, vehicle
from skills.routing import calculate_route
import ollama
### LLM Stuff ###
from langchain_community.llms import Ollama
from langchain.tools.base import StructuredTool
from skills import (
get_weather,
find_route,
get_forecast,
vehicle_status as vehicle_status_fn,
search_points_of_interests,
search_along_route_w_coordinates,
do_anything_else,
date_time_info
)
from skills import extract_func_args
global_context = {
"vehicle": vehicle,
"query": "How is the weather?",
"route_points": [],
}
MODEL_FUNC = "nexusraven"
MODEL_GENERAL = "llama3:instruct"
RAVEN_PROMPT_FUNC = """You are a helpful AI assistant in a car (vehicle), that follows instructions extremely well. \
Answer questions concisely and do not mention what you base your reply on."
{raven_tools}
{history}
User Query: Question: {input}<human_end>
"""
def get_prompt(template, input, history, tools):
# "vehicle_status": vehicle_status_fn()[0]
kwargs = {"history": history, "input": input}
prompt = "<human>:\n"
for tool in tools:
func_signature, func_docstring = tool.description.split(" - ", 1)
prompt += f'Function:\n<func_start>def {func_signature}<func_end>\n<docstring_start>\n"""\n{func_docstring}\n"""\n<docstring_end>\n'
kwargs["raven_tools"] = prompt
if history:
kwargs["history"] = f"Previous conversation history:{history}\n"
return template.format(**kwargs).replace("{{", "{").replace("}}", "}")
def use_tool(func_name, kwargs, tools):
for tool in tools:
if tool.name == func_name:
return tool.invoke(input=kwargs)
return None
tools = [
StructuredTool.from_function(get_weather),
StructuredTool.from_function(find_route),
# StructuredTool.from_function(vehicle_status),
StructuredTool.from_function(search_points_of_interests),
StructuredTool.from_function(search_along_route_w_coordinates),
StructuredTool.from_function(date_time_info),
StructuredTool.from_function(do_anything_else),
]
# llm = Ollama(model="nexusraven", stop=["\nReflection:", "\nThought:"], keep_alive=60*10)
# Generate options for hours (00-23)
hour_options = [f"{i:02d}:00" for i in range(24)]
def set_time(time_picker):
vehicle.time = time_picker
return vehicle.model_dump_json()
def get_vehicle_status(state):
return state.value["vehicle"].model_dump_json()
def run_generic_model(query):
print(f"Running the generic model with query: {query}")
data = {
"prompt": query,
"model": MODEL_GENERAL,
"options": {
# "temperature": 0.1,
# "stop":["\nReflection:", "\nThought:"]
}
}
out = ollama.generate(**data)
return out["response"]
def run_model(query):
print("Query: ", query)
global_context["query"] = query
global_context["prompt"] = get_prompt(RAVEN_PROMPT_FUNC, query, "", tools)
print("Prompt: ", global_context["prompt"])
data = {
"prompt": global_context["prompt"],
# "streaming": False,
"model": "nexusraven",
# "model": "smangrul/llama-3-8b-instruct-function-calling",
"raw": True,
"options": {
"temperature": 0.5,
"stop":["\nReflection:", "\nThought:"]
}
}
out = ollama.generate(**data)
llm_response = out["response"]
if "Call: " in llm_response:
func_name, kwargs = extract_func_args(llm_response)
print(f"Function: {func_name}, Args: {kwargs}")
if func_name == "do_anything_else":
return run_generic_model(query)
return use_tool(func_name, kwargs, tools)
return out["response"]
# to be able to use the microphone on chrome, you will have to go to chrome://flags/#unsafely-treat-insecure-origin-as-secure and enter http://10.186.115.21:7860/
# in "Insecure origins treated as secure", enable it and relaunch chrome
# example question:
# what's the weather like outside?
# What's the closest restaurant from here?
with gr.Blocks(theme=gr.themes.Default()) as demo:
state = gr.State(
value={
# "context": initial_context,
"query": "",
"route_points": [],
}
)
with gr.Row():
with gr.Column(scale=1, min_width=300):
time_picker = gr.Dropdown(
choices=hour_options,
label="What time is it? (HH:MM)",
value="08:00",
interactive=True,
)
history = gr.Radio(
["Yes", "No"],
label="Maintain the conversation history?",
value="No",
interactive=True,
)
voice_character = gr.Radio(
choices=[
"Morgan Freeman",
"Eddie Murphy",
"David Attenborough",
"Rick Sanches",
],
label="Choose a voice",
value="Morgan Freeman",
show_label=True,
interactive=True,
)
emotion = gr.Radio(
choices=["Cheerful", "Grumpy"],
label="Choose an emotion",
value="Cheerful",
show_label=True,
)
origin = gr.Textbox(
value="Luxembourg Gare, Luxembourg", label="Origin", interactive=True
)
destination = gr.Textbox(
value="Kirchberg Campus, Luxembourg",
label="Destination",
interactive=True,
)
with gr.Column(scale=2, min_width=600):
map_plot = gr.Plot()
# map_if = gr.Interface(fn=plot_map, inputs=year_input, outputs=map_plot)
with gr.Row():
with gr.Column():
recorder = gr.Audio(
type="filepath", label="Input audio", elem_id="recorder"
)
input_text = gr.Textbox(
value="How is the weather?", label="Input text", interactive=True
)
vehicle_status = gr.JSON(
value=vehicle.model_dump_json(), label="Vehicle status"
)
with gr.Column():
output_audio = gr.Audio(label="output audio")
output_text = gr.TextArea(value="", label="Output text", interactive=False)
# iface = gr.Interface(
# fn=transcript,
# inputs=[
# gr.Textbox(value=initial_context, visible=False),
# gr.Audio(type="filepath", label="input audio", elem_id="recorder"),
# voice_character,
# emotion,
# place,
# time_picker,
# history,
# gr.State(), # This will keep track of the context state across interactions.
# ],
# outputs=[gr.Audio(label="output audio"), gr.Textbox(visible=False), gr.State()],
# head=shortcut_js,
# )
# Update plot based on the origin and destination
# Sets the current location and destination
origin.submit(
fn=calculate_route,
inputs=[origin, destination],
outputs=[map_plot, vehicle_status],
)
destination.submit(
fn=calculate_route,
inputs=[origin, destination],
outputs=[map_plot, vehicle_status],
)
# Update time based on the time picker
time_picker.select(fn=set_time, inputs=[time_picker], outputs=[vehicle_status])
# Run the model if the input text is changed
input_text.submit(fn=run_model, inputs=[input_text], outputs=[output_text])
# close all interfaces open to make the port available
gr.close_all()
# Launch the interface.
if __name__ == "__main__":
demo.launch(debug=True, server_name="0.0.0.0", server_port=7860, ssl_verify=False)
# iface.launch(debug=True, share=False, server_name="0.0.0.0", server_port=7860, ssl_verify=False)
|