import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): # Construct the messages with prompt engineering messages = [{"role": "system", "content": system_message}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) # Add the user message messages.append({"role": "user", "content": message}) # Generate the response response = "" for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content response += token # Check if the response is empty or if required fields are missing if not response.strip(): yield "No relevant information could be generated based on the provided query." yield response # Enhanced system message for structured output default_system_message = ( "You are part of a geospatial AI agent/assistant and are aiding in parsing the query. When a user asks something," "You are a specialized parser for a geospatial AI assistant that analyzes city open data. " "Your task is to extract structured information from user queries about city data. " "Respond with the following format ONLY (no other text):\n\n" "city: [City Name or blank if unclear]\n" "state: [State Name or blank if unclear]\n" "dataset1: [Primary Dataset Type]\n" "dataset2: [Secondary Dataset Type if applicable, otherwise leave blank]\n" "time_filter: [any time filters mentioned - today, yesterday, last_week, last_month, this_year, etc.]\n" #"spatial_filter: [any spatial filters from - proximity, Boundary, near, within or distance]\n" "count_filter: [any count or limit filters]\n" "operations: [any sequencial GIS operations needed from - append_gdfs, calculate_aspect, buffer_features, calculate_field, calculate_geometry_attributes, clip_features, coordinate_system_transformation, dissolve_features, excel_to_table, export_to_geojson, feature_to_point, generate_near_table, geocode_address, reverse_geocode, create_heatmap, intersect_features, join_field, map_layout, merge_datasets, near_analysis, network_analysis_suite, project_features, service_area, shortest_path, slope_calculation, spatial_data_validation, spatial_join, spatial_statistics_hotspot, split_features, symbolization, table_to_excel, union_features]\n\n" "Provide only the extracted information without explanation. If information is not present in the query, leave that field blank." ) demo = gr.ChatInterface( respond, additional_inputs=[ gr.Textbox(value=default_system_message, label="System message"), gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ], ) if __name__ == "__main__": demo.launch()